- Home /
Unassigned reference exception on an assigned variable ?
Currently in my game, I have a mainMenu_UI object that contains a main menu script, and controls the menu. Usually, pressing "start game" works fine, however, in my main menu I have an options button, making the main menu.setactive = false, and the options menu.setactive = true. When I press back to return to the main menu, everything seems normal until i press "new game". At this point, the main menu is meant to be de-activated (the ui manager is inside of a "dontDestroyOnLoad", so it persists into the new scene). Unity does not deactivate the menu, and rather throws an error of "unassigned reference exception", referring to my mainMenu_UI object reference in my mainMenu script. Looking in the inspector, I can see that this variable is indeed assigned, and i am assigning it through the inspector at runtime. I dont know what to do as it is point to the problem but i just dont see what is wrong. There is no problem clicking straight onto new game, it seems to only hap-pen after the menu is de-activated then re-activated.Any help would be appreciated !
Answer by TheGalacticShaman · Feb 15, 2019 at 09:45 AM
Just fixed the issue, if any one else is experiencing this, double check to make sure that you haven't applied the script to another object accidentally. I must have dragged it onto another game object without noticing, so there was a duplicate script that was missing the reference.
Bro, you've no idea how much you've just helped me, thank you!!!!
Answer by tormentoarmagedoom · Feb 15, 2019 at 09:41 AM
Good day.
If you have a script with dontDestroyOnLoad, all its variables needs to be refeared by code, not via inspector... When scene changes, all objects are destroyed, so you need to refear them again, so must be the code itself who find and assign the variables again.
IF not, they will became null and this error appears.
Bye.
This was not the issue, as I was not trying to access a destroyed object. But thank you for taking the time to reply ! :)
Answer by Zorkind · Apr 26, 2020 at 01:06 AM
Dude WTF how did i did that? D:
I just did what you did, somehow the script showed up on an object on another scene that i was not even working on :-(
Ghosts in here OwO
Answer by yengNami · Feb 16, 2021 at 03:33 AM
There are also instances where you detached the script/ or copy pasted component from one object to another. and transferred it to another game object, then you forgot to delete old script attached the old game object. so double check it first.