Better solution for increasing ammo?

Hey!

I just watched the Ammo Pickup - Part 2 Lecture and I had another idea on how to increase the current ammo.

[SerializeField] int ammoAmount = 5;
[SerializeField] AmmoType ammoType;


void OnTriggerEnter(Collider other) {
    if(other.gameObject.tag == "Player"){
        other.GetComponent<Ammo>().IncreaseCurrentAmmo(ammoType, ammoAmount); 
        Destroy(gameObject);
    }
}

Instead of using FindObjectOfType() I just used other.GetComponent(). This should be better performance-wise and it would also work if you had multiple GameObjects that use the Ammo script. (For example another player). Or is there a problem with this solution that I don’t see yet?

3 Likes

Privacy & Terms