Null Reference Exception anytime scene is reloaded?
Im learning unity and have hit a road block that I cant figure out. I've searched for answers to this but nothing works for my situation.
I have an PoolManager GameObject (With PoolManager script), that creates the objects used at start. Also I have a LevelGenerator that grabs the inactive objects and sets them to active at the location set in script. LevelGenerator's script contains public PoolManager theObjectPool
, which I link the PoolManager to in unity.
This system works and objects are created as they should and are set active at the right time. But when I save and reload the scene, nothing spawns and I get a Null Reference Exception. Both the LevelGenerator and the PoolManager are still in scene/active and the objects were created and set to inactive. theObjectPool is still linked with PoolManager.
When I delete the PoolManager object and import it back from the prefabs and then relink theObjectPool with PoolManger, the scene works again as it should. The only issue is when the scene reloads or I go from my Main Menu to the game Scene. Any ideas what could cause this issue? Ive tried DontDestroyOnLoad in an Awake Function but didn't work. Also I placed a dubug.log in the PoolManager getPoolObject() function and even with the null reference error, the log shows up. Thank you for reading, let me know if more information is needed.Answer by donvaca · Feb 24, 2018 at 01:46 AM
I finally figured it out after hours of searching. Incase anyone else runs into this error, I had the game objects being instantiated and new lists of objects created within the Start() function. I changed this to Awake() and it worked
Your answer
Follow this Question
Related Questions
How do I fix a Null Reference Exception (object reference not set to an instance of an object)? 2 Answers
ObjectReference Not set to an instance of an object 1 Answer
Android Build Failure: NullReferenceException 1 Answer
NullReferenceException when attempting to call a GameObject from within PointerEventData 1 Answer
Coroutine Issue 1 Answer