Destroy by tag problem

Hi everyone,

i have simple script that instantiate array of objects, tag them to “spawn” and then destroys them after 20 seconds if there is no collision. If they collide with my main character, they become children and the tag changes into “picked”.

My problem is that even if tag changes, they get destroyed.
I used

  • Destroy(gameObject.FindWithTag(“spawn”), 20);

I checked in the inspector panel, the object becomes a child, and tag changes into “picked”.
If it’s necessary i will post whole script.
Does anyone know what seems to be the problem?

Thanks

Because as soon as you call Destroy that object is doomed. Call it after 20 seconds instead of right away.

e.g.

var spawn : GameObject = gameObject.FindWithTag("spawn") as GameObject;
yield WaitForSeconds(20);
if(spawn.tag == "spawn"){
   Destroy(spawn);
}

Thanks man, you solved my problem.

Thanks a lot!!!