- Home /
Destroy the current GameObject?
How do you destroy the current GameObject? (the one the script is in)
Destroy(GameObject)
doesn't work nor does Destroy(this)
... The latter seems to destroy the construct, but does not remove the actual GameObject onscreen...
When using Destroy(gameObject)
, it seems I am no longer able to instantiate new ones if I kill off the only prefab on screen (even from another script).
BTW - what is the difference between GameObject
and gameObject
and this
?
GameObject is the GameObject class, gameObject is a property which retrieves the GameObject attached to the script, this is an instance of the script
This question is probably better broken down into different topics... http://answers.unity3d.com/questions/33473/destroy-the-first-instanced-prefab-while-still-being-able-to-generate-more-prefab and http://answers.unity3d.com/questions/33474/difference-between-gameobject-and-gameobject-and-a-scene-item-named-gameobject
Answer by _Petroz · Dec 27, 2010 at 06:30 AM
Destroy(gameObject) is the correct way to the GameObject your script is attached to.
When using Destroy(gameObject), it seems I am no longer able to instantiate new ones if I kill off the only prefab on screen (even from another script).
I'm not sure what you mean by 'on screen'. Prefabs do not exist inside the scene, they are in the project. You should not be destroying Prefabs.
what is the difference between GameObject and gameObject and this?
GameObject is a class.
gameObject is a property you can call on a MonoBehaviour to the the GameObject instance that Monobehaviour instance is attached to.
this is the instance of the class the code is in.
prefab is just a reusable game object that once you create in unity and if you create it as a prefab, all the properties of the gameobject are saved and it can be reused in multiple scenes as it is.
Answer by B-rad · Nov 07, 2012 at 08:06 PM
Have you tried: Destroy(this.gameObject);
This should be the same as Destroy(gameObject) though right?
Answer by Jesus_Freak · Dec 27, 2010 at 04:17 AM
Destroy(gameObject) does (should) gt rid of attached gameobject
From a diff object you could say
Destroy(gameObject.FindWithTag("tag"));
Or something like that.
If you have just 1 prefab onscreen, that seems to get rid of all instances though - so that you can't call Instantiate on it again from another script
It gets rid of a single instance, the one the script is attached to (it's actually Component.gameObject in the base class of your script). If all of your objects are dying, you're calling it on different objects
Answer by Unityart3dsMAX · Jan 07, 2013 at 07:21 PM
Ok here you go
This script goes with the gameobject that is Instantiating the gameobject.
var bullet : GameObject;
var backup : GameObject;
function Update () { if(Input.GetKey("a")) backup = Instantiate(bullet, transform.position, transform.rotation);
}
The bak up file will make it to were if the gameobject gets destroyed then it will still be able to be Instantiated.
Im running out of time bu if you want to know more simple ways Email me
Email : unityart3ds@gmail.com
I will explain the simplest way to do it cause it is simple
So pleas Email me... :)(: ...
Answer by sriram90 · Dec 27, 2010 at 06:32 AM
this gameobject is nothing but in which gameobject you have placed that script...if you use Destroy(this.gameObject);
it'll destroy the gameobject in which you have placed the script....
Destroy(gameObject)- it'll destroy particular gameobject what you have specified.
try to store instantiated gameobject name as "clone" and destroy that clone object using Destroy(clone); it'll destroy all objects from origin.
that only works if you have one instantiated objects - what if you have more than one
actulally are you creating multiple objects in a same time?? can you give more explain about your concept? and what you're looking for??
why cant you try array....store that into "clone[i]" and Destroy(clone[i]); if you want delete all instantiated prefabs....
okay so loop through the entire array - no way to just set clone=Array();
- 1 line delete all?
Your answer
Follow this Question
Related Questions
Cannot destroy child object in prefab- Error 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Cannot cast from source type to destination type 1 Answer
PlayerRespawn class wont Instantiate the player prefab 1 Answer
Replace GameObject vs. replacing mesh and material? 2 Answers