How to destroy a clone (instantiate in scene) in the their own script

first, sorry for bad english,

Im having trouble trying to destroy a clone, my clone have a script attached to him, so in the script i have this code:

if  (this.transform.position.y <= MainCameraScript.altura.y - 90){

    Destroy(this.gameObject);
}

that code actually destroy the gameobject, but dont destroy the prefab clone and i have a bunch of empty clones.

any can help me? thanks in advance

1 Answer

1

You will need a reference to the object you’re trying to destroy.

When you instantiate a prefab, the call returns a reference to the newly created GameObject. If you need to store several of these, you could consider a collection like an array or list.

so, there is no way to destroy the clone in their own attached script? :s

You can destroy any object from any script -- just call Destroy() with a reference to the target. It's really more of a question of deciding which object to destroy when. "Self-destruct" calls, in which a script destroys itself or the GameObject it's attached to, are pretty common... but that's not a requirement. Which objects are you trying to destroy, and when do you want to destroy them? Once you've answered that, the path gets a little bit less murky.

ok, thanks. I think i need to put the destroy code in the "spawnScript" for do it more easy. Thanks.