Menu
CoddyTech

Crates Keep the Rest

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 18 of 71.

challenge icon

Challenge

Easy

Let a crate keep the ammo that did not fit.

  1. In AmmoCrate.cs, replace the AddAmmo line with amount -= blaster.AddAmmo(amount);.
  2. Call Destroy(gameObject); only when amount == 0.

Press Play and walk right: Crate2 stays, holding the 2 that did not fit. Then Check.

Try it yourself

using UnityEngine;

public class AmmoCrate : MonoBehaviour
{
    public int amount = 3;

    void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent(out Blaster blaster))
        {
            blaster.AddAmmo(amount);
            Destroy(gameObject);
        }
    }
}

All lessons in Physics and Gameplay

Practice on your own: Unity playground