Destroying distinct gameobject?

Hello. I’m making good progress on my latest game. I use tagging for destroying other gameobjects.
However, when touching an enemy, it often destroys another instance of the object, than the one the Player is touching.

This is the player touching the enemy

else if (other.CompareTag("Enemy_Carnivore"))
            if (isLethal)
                Instantiate(scorePlus, new Vector3(transform.position.x, transform.position.y + 1.5f, transform.position.z), Quaternion.identity);

And this is the code for destroying the enemy:

public void death()
        Destroy(gameObject, 0.7f);
        AudioSource.PlayClipAtPoint(damage, Camera.main.transform.position, damageSoundVolume);

If i put destroy() on the player part, it destroys the distinct enemy, however, the death animation of the enemy is never triggered.

Is there a way to destroy the distinct instance of the enemy where the animation is also part of it?

Hi Lord_Toby,

Welcome back to our community! :slight_smile:

FindObjectOfType returns the first object of a type Unity finds in your scene, whatever Unity defines as “first”. At the moment, your code probably returns a “random” EnemyCarnivore object. You, however, need to access the EnemyCarnivore component on the game object assigned to other. Look the collision method you are using up in the API and check its parameter to learn what you could access via the other variable.

Did this help?

See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms