- Home /
cannot destroy transform
public Object firstPiece;
firstPiece = Instantiate(levelPieces[startinglevelPiece],levelPieceSpawnPointL.transform.position, levelPieceSpawnPointL.transform.rotation);
Destroy(firstPiece);
These are the three line where I use the firstpiece variable directly and yet every time I go to destroy it I get this error "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 have tried casting it as a game object and I have tried everything besides directly tagging the object and destroying the objects with that tag, however I KNOW this should be able to work simply like this, what the hell am I doing wrong, am I high or missing some glaring problem (and I have tried changing the "public Object firstPiece;" line to GameObject that gives me lots more errors and problems than it fixes
Yes gives me "error CS1503: Argument #1' cannot convert
object' expression to type UnityEngine.Object'" " error CS1502: The best overloaded method match for
UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments " and "error CS1061: Type UnityEngine.Object' does not contain a definition for
gameObject' and no extension method gameObject' of type
UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?) "
To explain a little more firstpeice is a prefab that I instantiate when the player gets to a certain position and destroy when he is so far away if that helps, I am still trying with no results
I used $$anonymous$$arowi's advice and it worked. But hey, it IS confusing, cause in the script reference it doesn't explicitly says that you can access the gameObject through the transform. Anyway, thanks $$anonymous$$arowi.
Answer by loopyllama · Apr 15, 2011 at 08:03 AM
I bet firstPiece is a Transform. Your code "public Object firstPiece;" then casts it to an Object which indeed does not have a ".gameObject" property. This also explains why your GameObject cast did not work...
Destroy can destroy components or entire game objects. However, you cannot remove the transform component of a game object which is why you got your initial error.
instead of:
Destroy(firstPiece);
try:
public Object firstPiece;
firstPiece = Instantiate(levelPieces[startinglevelPiece],levelPieceSpawnPointL.transform.position, levelPieceSpawnPointL.transform.rotation);
Destroy( (firstPiece as Transform).gameObject );
but that code is not pretty. You might want to fix your "public Object firstPiece;" line and post a question about those errors...you probably don't need to cast your Transform to an Object, which forces you to cast it back to a Transform and access the gameObject to destroy it.
That line does work better, however I go to reuse it soon after to destroy a new Object put into the firstPieceVariable and I get
"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."
and changing the "public Object firstPiece;" line to GameObject gives me errors about missing castes or explicit conversions with my instantation line, is there a quick way to get rid of those? perhaps I am not doing the castes right
if you need firstPiece to not be destroyed, do not destroy it. Just remove the entire destroy line. You need to post the public object errors in a new question. The forums are the place for an elaborate detailed discussion.