NullReferenceException: Object reference not set to an instance of an object
Hi, I'm relatively new to unity and I have just gotten the error NullReferenceException: Object reference not set to an instance of an object which I have managed to deal with in the past, but what I don't understand is that my script is working exactly as it should yet I am still getting this error. I have made this script to set three menus false whilst the user is playing my game and it starts fine and hides the menus from the user but I still get this error for some reason and I can't seem to find a solution The exact error is: NullReferenceException: Object reference not set to an instance of an object DeathMenuManager.NoMenus () (at Assets/Scripts/Managers/DeathMenuManager.cs:44) DeathMenuManager.Start () (at Assets/Scripts/Managers/DeathMenuManager.cs:22)
Hello
I think in your function No$$anonymous$$enus() you should use the local reference Death$$anonymous$$enu, Respawn$$anonymous$$enu... ins$$anonymous$$d of using Find() again.
I did that initially and got the same error
Well that is weird you get the error on line 44 and not on line 19, do you have other scripts affecting those menus? because if the menus are inactive Find() is not going to work.
Answer by Falcon8857 · Feb 17, 2016 at 03:17 PM
Probably when starting the object there is no menu, but you are not using the reference there so DeathMenuManager.Start doesn't us the null pointer. But you call NoMenus in the same Start, and there you use the reference of the menu (wich hasn't been created yet) and that's why you get the null pointer reference error. If you do if (GameObject.Find("DeathMenu") == null) Debug.Log("No menu loaded!"); you will understand it better.
Thanks for the reply, I'm not to sure what you mean by the null pointer, like I said i'm still new to unity but I figured out why the error was occurring, I accidentally put the script in the scene twice so the start function was running twice and thus the void "No$$anonymous$$enus" was running twice causing the error
Your answer
Follow this Question
Related Questions
Keep getting Null reference error for AI Script 1 Answer
Unity 2017 playmode is not working 0 Answers
IndexOutOfRangeException in TextureInspector 1 Answer
grandle build fail 0 Answers