- Home /
Destroy GameObject Variable?
var Zap : GameObject;
function Start() {
Destroy(Zap);
}
My code is shown above. I place the game object Zap into that variable slot and then i get the error "The name 'Zap' does not denote a valid type ('not found')
I just want to destroy the OBJECT!!
I just tried the code and it worked fine for me perhaps you need to drag and drop the game object you want to destroy into the script?(add it to a random object then drag the object you want to destroy from wherever it is in the hierarchy tab into the inspector script)
Answer by clunk47 · Dec 24, 2013 at 01:57 AM
Why would you guys want him to declare it as a new gameObject when it's obviously a public variable? Just be sure since your variable is public, you assign the gameObject in the inspector, then check that it's not null before you destroy it.
#pragma strict
var Zap : GameObject;
function Start()
{
if(Zap != null)
Destroy(Zap.gameObject);
}
because if he is so new he doesnt know how that works i've no idea if its an assigned public variable.
Well then tell him to assign it in the inspector? If you want to assign at runtime via code,
private var Zap : GameObject = new GameObject();
function Start()
{
if(Zap != null)
Destroy(Zap.gameObject);
}
Or just Destroy(Zap), since it is already defined as a GameObject.
Answer by sparkzbarca · Dec 24, 2013 at 12:45 AM
zap must not actually be equal to anything it must be null already.
basically if you do for example
Zap : GameObject = new GameObject();
then Destroy(Zap)
or you assign Zap through the editor window to something it'll work
but basically what you need to understnad is declaring a variable doesnt make it equal to something or do anything nessacarily.
It's confusing because some objects like numbers have a built in constructor so when you do for example
int x;
x actually exists now it is in fact a number with its default value which happens to be 0 thats coded in, when an int is created it automagically has a value
but not all classes do except a few intergral classes things like gameobjects require the new keyword to be assigned memory.
Answer by Cheetoturkey · Dec 24, 2013 at 01:10 AM
Zap : GameObject = new GameObject;
Destroy(Zap.gameObject)
Your answer
![](https://koobas.hobune.stream/wayback/20220613124749im_/https://answers.unity.com/themes/thub/images/avi.jpg)