- Home /
How to access GameObject under DontDestroyOnLoad in Hierarchy at run time
Hello
I have been trying to access a gameobject called LevelManager which contains a script called LevelManager which has an enum I want to use to change scenes. This gameobject is a child of an empty gameobject called Persistant Objects which gets instantiated at run time from a script that also sets the instantiated objects as DontDestroyOnLoad.
Basically I have a scene called Splash Screen which is the first scene you see when you hit Play. There is a button which you click to play the game. This button has an attached script with a function called LoadLevelOne()
Every time I click on the Play Game button I get the error Object Reference not set to an instance of an object. I want to do is load the Level One scene but am having great difficulty trying to access Level Manager. Have tried so many different ways to try an access this but have had no success.
Cant I access gameobjects that are under DontDestroyOnLoad?
Answer by KloverGames · Apr 29 at 03:50 AM
Because it doesn't look like it's active... You can't find game objects that aren't active in the scene. You have to make sure it's always active if you want to access it with GameObject.Find();
Answer by TheIrishKraken · Apr 29 at 04:15 AM
I wish that was the case but the gameobject is active. Running gameObject.activeInHierarchy will tell you if a gameobject is active in the scene or not
Persistent Objects which is the parent of Level Manager is definitely inactive
To be fair it does look inactive based on its faded text colour but it has a tick next to it in the hierarchy. And if it is actually inactive I cant understand what would make it so, all I did was instantiate it & set it to dontdestroyonload.
Make sure the prefab itself is active in the assets folder where you’re instantiating it from. Sometimes I would forget and when it got instantiated, I wondered why it didn’t show up
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
creating json for waypoints and links to them 0 Answers
Setting public GameObject to a different Prefab through code 0 Answers