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

Getting "MissingReferenceException: The variable of doesn't exist anymore" error after calling Set

Discussion in 'Scripting' started by 3dGeek, Aug 8, 2017.

  1. 3dGeek

    3dGeek

    Joined:
    Oct 5, 2012
    Posts:
    10
    I'm seeing this weird issue in my project all of the sudden. I'm actually using a function from the Network Lobby package from the Asset Store. The code looks like this.
    Code (csharp):
    1.  
    2.     public void ChangeTo(RectTransform newPanel)
    3.     {
    4.         if (currentPanel != null)
    5.         {
    6.             currentPanel.gameObject.SetActive(false);
    7.         }
    8.  
    9.         if (newPanel != null)
    10.         {
    11.             newPanel.gameObject.SetActive(true);
    12.         }
    13.  
    14.         currentPanel = newPanel;
    15.     }
    16.  
    ChageTo() is supposed to turn the current panel off and switch the new panel on. But, when I call ChangeTo() and pass an instance of a panel, it throws the error message and I can't set the panel active anymore. Does anyone have any idea what is going on here?

    Thanks,

    Mike
     
    Last edited: Aug 9, 2017
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Are you destroying these panels anywhere? Perhaps you're going from one scene to another, where the old panel belongs to the old scene?

    That error usually happens when you're trying to access a gameobject that's been destroyed. I don't see anything happening in the code you posted that could cause that, so it must be elsewhere in your project.
     
  3. 3dGeek

    3dGeek

    Joined:
    Oct 5, 2012
    Posts:
    10
    Everything is done in one scene. I should have included the rest of my code in my original post. ChangeTo() gets called in a button click handler like this.

    public RectTransform mainPanel;

    public void mainMenuButtonHandler()
    {
    ChangeTo(mainPanel);
    }

    As soon as currentPanel.gameObject.SetActive(false) gets called and turns the last panel off, the error happens. Not only does the old panel turn invisible. But, all references to it go away

    Thanks,

    Mike
     
  4. 3dGeek

    3dGeek

    Joined:
    Oct 5, 2012
    Posts:
    10
    I created a small test project that just included the button handlers and the ChangeTo() method. And, it worked fine. So, it looks like there is an issue with my project. To try to figure out what is going on, I added some debug logic. Now it looks like this.

    Code (csharp):
    1.  
    2.     public void GameTabHandler()
    3.     {
    4.         Debug.Log("entered GameTabHandler()");
    5.         ChangeTo(pnlGame);
    6.         Debug.Log("leaving GameTabHandler()");
    7.     }
    8.  
    9.     public void ChangeTo(RectTransform newPanel)
    10.     {
    11.         Debug.Log("entered ChangeTo()");
    12.         if (currentPanel != null)
    13.         {
    14.             Debug.Log("setting currentPanel.SetActive(false) ");
    15.             currentPanel.gameObject.SetActive(false);
    16.             Debug.Log("done");
    17.         }
    18.  
    19.         if (newPanel != null)
    20.         {
    21.             Debug.Log("setting newPanel.SetActive(true) ");
    22.             newPanel.gameObject.SetActive(true);
    23.             Debug.Log("done");
    24.         }
    25.  
    26.         Debug.Log("currentPanel = newPanel;");
    27.         currentPanel = newPanel;
    28.         Debug.Log("leaving ChangeTo()");
    29.     }
    30.  
    When I click my Game Tab button, my output looks like this.

    entered GameTabHandler()
    entered ChangeTo()
    setting currentPanel.SetActive(false)
    MissingReferenceException: The variable pnlCalibrate of VBTGame doesn't exist anymore.

    The rest of the method does not get executed. The rest of the script does continue to run though.

    It appear that something about this game is breaking the SetActive() method. Does anyone have any ideas on how to troubleshoot this?

    Thanks,

    Mike
     
    Last edited: Aug 9, 2017
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
  6. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    What is
    Code (csharp):
    1. public void GameTabHandler()
    ?

    Is that a constructor? If so, change it to Awake().
     
  7. 3dGeek

    3dGeek

    Joined:
    Oct 5, 2012
    Posts:
    10
    Code (csharp):
    1.  
    2. public void GameTabHandler()
    3.  
    Is called by a button on the base panel.
     
  8. 3dGeek

    3dGeek

    Joined:
    Oct 5, 2012
    Posts:
    10
    It looks like the issue is due to a bug within Unity. My script works for one UI hierarchy but fails for another. I decided to go ahead and use a work-around. I will post what I did in case anyone else runs into the same issue I did.

    I was trying to implement a tabbed-dialogue similar to the one used in the Network Lobby asset on the Asset Store here.

    https://www.assetstore.unity3d.com/en/#!/content/41836

    I tried using currentPanel.gameObject.SetActive(false) to turn the last panel off and use newPanel.gameObject.SetActive(true) to turn the new panel on. That usually works. But, in my case, the SetActive(false) method causes Unity to lose its reference to the panel the user is navigating away from. I found that the same script works fine on a simple UI hierarchy but has issues with more complex ones.

    My solution was to write a script that avoids using SetActive(false) to turn UI panels off. Instead it moves unused panels off of the screen. i hate this work-around. But, its the only thing I could come up with given my time restraints.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class UIDirector : MonoBehaviour {
    7.  
    8.     public RectTransform pnlCalibrate;
    9.     public RectTransform pnlSetup;
    10.     public RectTransform pnlGame;
    11.  
    12.     private Dictionary<string, PanelData> dctPanels = new Dictionary<string, PanelData>();
    13.  
    14.     /// <summary>
    15.     ///
    16.     /// </summary>
    17.     void Start()
    18.     {
    19.         RegisterPanel(pnlCalibrate);
    20.         RegisterPanel(pnlSetup);
    21.         RegisterPanel(pnlGame);
    22.  
    23.         SwitchTo(pnlCalibrate);
    24.     }
    25.  
    26.     /// <summary>
    27.     ///
    28.     /// </summary>
    29.     /// <param name="rctPanel"></param>
    30.     void RegisterPanel(RectTransform rctPanel)
    31.     {
    32.         dctPanels.Add(rctPanel.name, new PanelData(rctPanel));
    33.     }
    34.  
    35.  
    36.     /// <summary>
    37.     /// This code fires with the Calebrate Panel Tab button is clicked
    38.     /// </summary>
    39.     public void ActivateCalibratePanelButtonHandler()
    40.     {
    41.         SwitchTo(pnlCalibrate);
    42.     }
    43.  
    44.     /// <summary>
    45.     /// This code fires with the Setup Panel Tab button is clicked
    46.     /// </summary>
    47.     public void ActivateSetupPanelButtonHandler()
    48.     {
    49.         SwitchTo(pnlSetup);
    50.     }
    51.  
    52.     /// <summary>
    53.     /// This code fires with the Game Panel Tab button is clicked
    54.     /// </summary>
    55.     public void ActivateGamePanelButtonHandler()
    56.     {
    57.         SwitchTo(pnlGame);
    58.     }
    59.  
    60.     /// <summary>
    61.     /// Moves all unused tab panels off of the screen.
    62.     /// </summary>
    63.     /// <param name="newPanel"></param>
    64.     public void SwitchTo(RectTransform newPanel)
    65.     {
    66.         foreach (KeyValuePair<string, PanelData> kvpPanel in dctPanels)
    67.         {
    68.             PanelData pnl = kvpPanel.Value;
    69.             if (pnl.transform.Equals(newPanel.transform))
    70.             {
    71.                 pnl.MoveToOriginalPosition();
    72.             }
    73.             else
    74.             {
    75.                 pnl.MoveOffScreen();
    76.             }
    77.          
    78.         }
    79.      
    80.     }
    81. }
    82.  
    83.  
    84. using UnityEngine;
    85.  
    86. public class PanelData
    87. {
    88.     public RectTransform transform { get; set; }
    89.     public Vector3 location { get; set; }
    90.     public Quaternion orientation { get; set; }
    91.  
    92.     private Vector3 vctOffScreen = new Vector3(100000, 0, 0);
    93.  
    94.     public PanelData(RectTransform tran)
    95.     {
    96.         this.transform = tran;
    97.         this.location = tran.position;
    98.         this.orientation = tran.rotation;
    99.     }
    100.  
    101.     public void MoveOffScreen()
    102.     {
    103.         Debug.Log(string.Format("moving {0} off screen {1} ", this.transform.name, vctOffScreen));
    104.         this.transform.SetPositionAndRotation(vctOffScreen, Quaternion.identity);
    105.     }
    106.  
    107.     public void MoveToOriginalPosition()
    108.     {
    109.         Debug.Log(string.Format("moving {0} to original position {1} ", this.transform.name, this.location));
    110.         this.transform.SetPositionAndRotation(this.location, this.orientation);
    111.     }
    112.  
    113. }
    114.  
    115.  
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    If you think you're encountering a bug, I'd file a report. I'd personally would like to try and reproduce your issue, as I haven't encountered anything like that before, even using a similar system that manages several different dialog boxes.

    Glad you found a work around at least.
     
  10. 3dGeek

    3dGeek

    Joined:
    Oct 5, 2012
    Posts:
    10
    Thanks @Brathnann. I would upload my project if i could. Unfortunately, the group I am writing this for has a patent on the application. So, I'm not sure that I can upload it without getting into legal trouble.

    I tried copying the project and chopping parts of it off to make something that I could upload. But, it began changing the behavior and I ran out of time.

    Thanks again for the input.
     
  11. Ziplock9000

    Ziplock9000

    Joined:
    Jan 26, 2016
    Posts:
    360
    FYI I got this error when I have a public GameObject I has on a script that I assigned via the inspector that I changed the type to transform. The object stayed there in the inspector, but the error occured.
     
  12. TabuuForteAkugun

    TabuuForteAkugun

    Joined:
    Sep 28, 2015
    Posts:
    56
    Getting this in Unity 2021.3.11f1. I have a global gameobject with DontDestroyOnLoad() with global variables on it, and it decided to stop working on me all of a sudden
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Anytime you destroy something that something else is still pointing at, and that something else accesses it, you get this.

    If you think it wasn't happening before because the earlier version of Unity didn't do it, you are likely incorrect, and in any case, it's not an excuse to start necro-posting to old threads for the simplest most common error in the entire world.

    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

    In the future, please start your own new thread, but you never even have to post ANYTHING for a NullReference. Just use the above three steps and fix it.