- Home /
"Destroy Game Object" Not Destroying Game Object
Ok, I have a problem. When I step on my Interactor a sound plays and some platforms are created. I only want one platform to be created, so I made it so that the interactor is destroyed after instantiating the platform. Except, it doesn't get destroyed.
var Demon1 : AudioClip;
var platform : GameObject;
var interactorObject : GameObject;
function OnControllerColliderHit(hit: ControllerColliderHit) {
if(hit.collider.gameObject ==
GameObject.Find("interactorObject")){
audio.PlayOneShot(Demon1);
Instantiate(platform, Vector3(transform.position.x, transform.position.y+2, transform.position.z), transform.rotation);
Destroy(interactorObject.GameObject);
}
}
Answer by robertbu · Aug 05, 2013 at 07:11 AM
Not sure here, but interactorObject is only valid if you initialize it in the inspector. A safer way would be to use the one from the hit. Plus you can look at the name of the hit. You don't have to do an expensive GameObject.Find(). Try this:
var Demon1 : AudioClip;
var platform : GameObject;
function OnControllerColliderHit(hit: ControllerColliderHit) {
if(hit.collider.name == "interactorObject") {
audio.PlayOneShot(Demon1);
Instantiate(platform, Vector3(transform.position.x, transform.position.y+2, transform.position.z), transform.rotation);
Destroy(hit.collider.gameObject);
}
}
Please post as a comment next time and not as an answer (I converted it for you).
If @robertbu helped you, please mark his answer as accepted.
Answer by Ambro · Aug 05, 2013 at 07:13 AM
Maybe I'm wrong, but I think it must be :
Destroy(interactorObject.gameObject);
instead of
Destroy(interactorObject.GameObject);
Answer by liszto · Aug 05, 2013 at 07:15 AM
Are you sure your interactorObject got a reference on something ?
Cause when you do your if statement to check if this one exist, you don't fill the interactorObject variable with something.
Did you try something like this :
if(hit.collider.gameObject ==
GameObject.Find("interactorObject"))
{
interactorObject = GameObject.Find("interactorObject");
//...
}
Or this :
Destroy(hit.collider.gameObject);
Hope this can help you.