Search code examples
c#unity-game-enginedestroy

Why does one Gameobject work when i try to destroy the other one, but it doesnt work the other way around?


I want a Gameobject to delete itself and a ifferent Gameobject with the tag 'Card' The Script is attached to both of them, so i expected it to work for both, but just one of them does it. I used this code The Code is called when i drop the Card so only one of the cards runs the code at a time

Destroy(gameObject);
Destroy(GameObject.FindWithTag("Card"));

I also tried adding gameObject.transform.SetAsLastSibling(); to see if it would change anything, but it didn't


Solution

  • I fixed it by creating an array with the gameObjects that i want to destroy and then destoying them in order

    GameObject[] destroy = GameObject.FindGameObjectsWithTag("Card");
            for (var i = 0; i < destroy.Length; i++)
            {
                Destroy(destroy[i]);
            }