- Home /
How to destroy a transform's parent object.
I'm doing the 3D Platformer tutorial.
I've got my CopperDead prefab, which is an entire dead copper model, but instantiated as a transform in the EnemyDamage.js script, here:
var deadModel = Instantiate(deadModelPrefab, transform.position, transform.rotation);
Onto my CopperDead prefab, I've added a new script, FadeOutAndDie.js. My goal is to not have the place littered with dead cops but, rather, for them to alpha-fade out, then be destroyed.
...Except I'm not allowed to
Destroy(gameObject);
since that points to a transform, at this point in time. So I tried doing
Destroy ((gameObject as Transform).parent);
but I get a null-pointer error (which is a bit confusing, but that's another question for after I've done more reading.)
So my question is: what's the thing I want to Destroy to get rid of the [dead] robot whose transform I happen to have in my gameObject variable?
Thanks!
Answer by Olie · Mar 24, 2010 at 05:16 AM
Ok, well... that was dumb. It turns out that all needed to do was change...
var deadModelPrefab : Transform;
var deadModel = Instantiate(deadModelPrefab, transform.position, transform.rotation);
to
var deadModelPrefab : GameObject;
var deadModel = Instantiate(deadModelPrefab, transform.position, transform.rotation);
D'oh! Then, in my FadeOutAndDie.js,
Destroy(gameObject);
works just fine.
Sorry about the dumb n00b question -- seems you always find the answer 10 sec after typing the question & hitting send, eh?
Not exactly always.. but next time give it a shot before asking a question if you are about to answer it in 10 seconds.. :)
http://answers.unity3d.com/faq feel free to read that also..!!
@Lipis: well, I didn't $$anonymous$$NOW I was going to think of something to try 10 $$anonymous$$utes later, or I would've tried it first! :)
Also, the more abstract question -- how do you find the parent GameObject to which a transform is tied -- still seems like it would be useful. There may be other instances where that crops up, no? It seemed odd to me that someTransform.parent didn't return what I expected. I imagine I'm not the only person just starting with Unity3D who doesn't understand that...
@Olie: No need to apologize - it's cool that you came back and answered your own question. After a little while UnityAnswers will allow you to mark your own answer as accepted (click the check mark) so remember to do that! :)