Better approach to select player on collision

There is better and more universal approach to find our player on collision. In some cases there could be more then one player (coop / multiplayer / switchable characters), so using FindObject could be random, and we don’t want it.

As we know there is collision detected, we also verified that object collided with player, we can just take our collider and modify it directly. We are sure here, we add ammo to player which collided.

It should also work faster, as we don’t have to look for object :slight_smile:

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

6 Likes

Privacy & Terms