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

51 Views Asked by At

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

2

There are 2 best solutions below

0
Theo Nimz On BEST ANSWER

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]);
        }
1
Ahmad .H Developer On

this line of executes inside the gameObject

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

but you are destroying the gameObject before it executes the second line of code it should be like this

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