- Home /
gameObject.find doesn't work
Hello,
I made this script to make an inactive object active if scene 1 is loaded + the variable gameMode is 2, but it doesn't work. It gives me this error. Any help?
NullReferenceException Js_Manager.OnLevelWasLoaded (Int32 level) (at Assets/Scripts/Js_Manager.js:15)
function OnLevelWasLoaded (level : int) {
if(level == 1)
{
if(gameMode == 2)
{
GameObject.Find("SpaceshipPlayer2").active = true;
}
}
}
Answer by gregzo · Jul 27, 2013 at 11:54 AM
GameObject.Find will not return inactive GameObjects.
And I really advise you not to find objects by name, it is slow and bad practise. Cache a reference to them instead.
easiest way is to declare a public GameObject or GameObject[], and then to drag and drop in the inspector. Quick and dirty, but better than GameObject.Find. Depends on your setup, really.
I can't do this because the gameObject with the script is generated dynamicly
Even better, just cache a reference as you generate it. Please post that as a seperate question if needed and accept this answer to keep things tidy.
Ok, thanks. I will post another question to ask how it's done! :)
Your answer
Follow this Question
Related Questions
Organize around inactive GameObjects not being findable 0 Answers
How to find Inactive GameObject 16 Answers
GameObject.Find() work on inactive objects 16 Answers
SetActive(true) not working 2 Answers
Find all gameObjects with same tag 1 Answer