- Home /
Why Can't I Destroy This Object?
Hi. I have a problem with the Destroy() function in my program. I know it takes a gameobject, but I still get the error below:
Can't destroy Transform component. If you want to destroy the game object please call 'Destroy' on the game object instead. Destroying the transform component is not allowed.
I don't know what it means. The only point where I have the Destroy() function is when I wrote,
Destroy(hairset);
Earlier I define hairset:
static var hairset : GameObject;
The only reason I could think of for this not working is the fact that it might not work except with the gameobject that the script is attached to, but I also don't see the logic in that. If this is true, then how can I destroy my variable? I just don't understand what it's talking about with the transform component.
Thank you for any help you can give.
you have only mentioned defining hairset. is it a null reference? also, do you really want an object that can change declared as "static" ?
Well, the given information tells use that you try to destroy a Transform component because you pass a reference to a Transform to the Destroy function. The error can't have any other reason.
So there are several possibilities:
You have another variable, maybe in another scope, called "hairset" and this is of type Transform.
It's not the given line that produces the error.
Some more context would help i guess. Also like crogers said, why is it static and where do you set it? It would also be useful to know where and when you call Destroy
Answer by Zantom07 · Jul 13, 2012 at 10:28 PM
Are you using the inspector to define what hairset actually is? If so, you can't attach gameobjects within the inspector, only transforms, so maybe you need to change the definition to this:
static var hairset : Transform;
And then the destruction to this:
Destroy(hairset.gameObject);
Answer by Piflik · Jul 13, 2012 at 10:07 PM
It should work if you type Destroy(hairset.gameObject). Although it might be possible that the static property of your variable interferes with this...
Answer by TheDarkVoid · Jul 13, 2012 at 10:29 PM
it would be best to call the Destroy() function on the object you want to destroy.
Answer by untityuserpoint0 · Jul 13, 2012 at 11:55 PM
I am the same user who asked the question. Just a different username. I tried using Zantom's advice (and Croger's advice. It is no longer static) and I'm not getting the same error instead it says, "MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object." Truth is... I never access it again. The route I might take is to just find another way to the solution instead of using Destroy. Is there any other way to get rid of an object without affecting the variable that it is attached to? What I should have explained was that I have two objects (hairset and hair1). Hairset is in the scene and Hair1 is a prefab that is not in the scene. I need to switch them out, but I need to do it so that the prefab, Hair1, is set as Hairset after the original Hairset object is gone. Is that possible?
Can you tell us what the numbers are in the brackets, something like:
$$anonymous$$issingReferenceException: blablahblah... (22,23)
That shows you the error is on line 22, column 23. This should help you pinpoint where your problem lies.