- Home /
GameObject.Find --> Destroy is not working
I have a function that is meant to find a game object ("AudioLoad") and then destroy it. The variable playingAudioLoad is updated as expected, and I had some print() commands in there that worked as well.
However, the GameObject isn't being destroyed and for the life of me, or perhaps because it's late, I can't figure out why. Any ideas?
Thanks so much!
function StopLoad ()
{
if (playingAudioLoad)
{
var nowPlaying : GameObject;
nowPlaying = GameObject.Find("AudioLoad");
Destroy (nowPlaying, 0);
playingAudioLoad = false;
}
}
Are you sure 'nowPlaying' is not null? Also note, that 'Destroy' might not work immediately, and you might check if the object was destroyed too early. You could try and use 'DestroyImmediate' although it is not recommended unless used in Editor scripts. http://docs.unity3d.com/Documentation/ScriptReference/Object.DestroyImmediate.html
Answer by Fattie · Dec 05, 2012 at 09:06 AM
function StopLoad()
{
Debug.Log("function StopLoad was called");
if (playingAudioLoad)
{
Debug.Log("playingAudioLoad is ok");
var nowPlaying : GameObject;
nowPlaying = GameObject.Find("AudioLoad");
if ( ! nowPlaying )
{
Debug.Log("could NOT find name AudioLoad");
}
else
{
Debug.Log("I was able to find AudioLoad ok");
}
Destroy (nowPlaying, 0);
playingAudioLoad = false;
}
else
{
Debug.Log("playingAudioLoad was false");
}
}
Thank you! I didn't realize the script wouldn't throw an error if !nowPlaying
I plugged in that code, saw that it was indeed unable to find the GameObject AudioLoad.
Turns out, since it's a prefab Instantiated, the actual name of the GameObject was AudioLoad(Clone) -- it is now working. Thank you!
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
GameObject.FindWithTag doesn't work! 1 Answer
create a destroyer for 2d infinite runner game objects. 2 Answers
Clones not being Destroyed 1 Answer
GameObject.Find can't find the GameObject "Jukebox" in another Scene 2 Answers