Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved NullReferenceException but the object is assigned

Discussion in 'Scripting' started by VengarlofForossa, Jun 13, 2023.

  1. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    My solution at the end.

    It was a redo of the scene from scratch. I copied and pasted the Menu scene and inserted the player and all the objects. I have done exactly the same and in the new scene it works.

    I don't understand the error, because when debugging both the button and the image were correct.

    When redoing the scene I didn't change the code either.

    ===========================================

    Hello everyone!

    I have created a Main Menu scene and in it I have created an Options button that opens the Options panel.

    I have created a gameobject to which I have added the Script Options. To this GameObject I have attached the necessary buttons and panels.

    And it works perfectly. Then I convert the Options panel and the GameObject with the Script in Prefab.

    In the game scene I create a Pause menu and include the options.

    I open the Pause menu and click Options. When I click on the Video button, it should change the colours of the buttons and open and close panels, but I get a NullReference error.

    When I do Debug.Log in Start(), I see that it is correctly added. I do Debug.Log in Update and it works. When I click on the Video button is when I get the error.

    What is the reason for this error? In the menu it works perfectly but in the game I get this error.

    If for example I comment out the code to change the colour of the buttons, the error I get is

    UnassignedReferenceException: The variable OptionsPanel of Options has not been assigned.

    Code (CSharp):
    1.  
    2. public void activeVideoPanel()
    3.     {
    4.         buttonSound.GetComponent<Image>().color = Color.white;
    5.         buttonVideo.GetComponent<Image>().color = new Color(0.47f, 0.97f, 0.4f, 0.65f);
    6.         buttonControl.GetComponent<Image>().color = Color.white;
    7.  
    8.         OptionsPanel.SetActive(true);
    9.         SoundPanel.SetActive(false);
    10.         VideoPanel.SetActive(true);
    11.         ControlPanel.SetActive(false);
    12.     }
    13.  
     
    Last edited: Jun 14, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    The answer NEVER changes... it is ALWAYS the same.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    Don't write code like this:

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums
     
  3. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    I edit the first message to add more information about the error.
    The code worked in a Menu, but not in a game scene.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    IT DOES NOT MATTER. The answer DOES NOT CHANGE.

    I'll post it again for you:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Well the code you posted does not have a Debug.Log or a line 327, so are you sure it has anything to do with the error?