- Home /
Question by
My-Second-Opinions-first · Oct 05, 2020 at 05:58 PM ·
2d2d-platformer2d-gameplaydestroy objectdestroygameobject
OnTriggerEnter2D Running twice when destroying game object.
I am trying to make a key and a door system with multiple players, where if one player gets a key the others are able to use it, the only problem I am running into is that the keys are getting collected twice by the same GameObject, and Destroy(other.gameObject)
is not working. Here is my code (Please note: playerManager is what stores the number of keys):
if (other.tag == "Key" && other.gameObject != null)
{
Destroy(other.gameObject);
Destroy(other);
playerManager.keys++;
}
if (other.tag == "Door" && other.gameObject != null)
{
if (playerManager.keys > 0)
{
Destroy(other.gameObject);
Destroy(other);
playerManager.keys--;
}
}
All help would be appreciated.
Comment
Best Answer
Answer by My-Second-Opinions-first · Oct 05, 2020 at 06:14 PM
I have solved my own problem! Here is the link to the solution: https://answers.unity.com/questions/1097482/how-do-you-get-ontriggerenter2d-to-activate-only-o.html