Search Unity

Need help with MissingReferenceException

Discussion in 'Getting Started' started by yoanpumar, Sep 13, 2020.

  1. yoanpumar

    yoanpumar

    Joined:
    Apr 19, 2020
    Posts:
    2
    Hi guys,

    I've been struggled in the last 2 days with a null reference exception and I don't know what is happening. Let me start saying that I'm new in game development, so it's possible that I'm doing something wrong :). I'm using only one scene with a State Manager class to handle different states and transitions between them. Everything is working fine until I try to enter the GamePlay state the second time. For instance, I can go through the main menu, start playing, stop the game play to comes back to the main menu, until here, everything is ok but if I want to go back to the game play state again, then I got the null reference error. I'm sharing details below to see if hopefully someone can help me with this because honestly, I don't know what else to do :(.

    GamePlayState
    Code (CSharp):
    1.  
    2. public class GamePlayState : BaseState
    3.     {
    4.         GamePlayStateGUI gameComponent;
    5.  
    6.          // Initialization method.  Called after the state
    7.         // is added to the stack.
    8.         public override void Initialize()
    9.         {          
    10.             Time.timeScale = 1.0f;
    11.             Screen.sleepTimeout = SleepTimeout.NeverSleep;
    12.             gameComponent = SpawnUI<Menus.GamePlayStateGUI>(Constants.PrefabGamePlayState);
    13.          
    14.             m_CountdownRectTransform = gameComponent.countdownText.GetComponent<RectTransform>();
    15.  
    16.             // Subscribe player spawn event
    17.             CommonData.mainGame.PlayerSpawnedEvent.AddListener(StartGame);
    18.  
    19.             // Spawn the player object          
    20.             CoroutineHandler.StartStaticCoroutine(CommonData.mainGame.SpawnPlayer());            
    21.         }
    22.  
    23.         public void StartGame()
    24.         {
    25.             gameComponent.wholeUI.gameObject.SetActive(true); // Here is where the null reference happens
    26.             // rest of game logic
    27.             .
    28.             .
    29.             .
    30.          }
    31. }
    GamePlayStateGUI
    Code (CSharp):
    1.  
    2.      // class for providing code access to the GUI
    3.     // elements in the Game State
    4.     public class GamePlayStateGUI : BaseMenu
    5.     {
    6.         // These fields are set in the inspector.      
    7.         [Header("UI")]
    8.         public TMP_Text coinText;
    9.         public TMP_Text scoreText;      
    10.         public TMP_Text multiplierText;
    11.         public TMP_Text countdownText;
    12.         public RectTransform wholeUI;
    13.         public GUIButton pauseButton;
    14.     }
    15.  
    This is really weird, the second time I enter the GamePlayState, gameComponent is spawned correctly inside the Initialize method, I debugged it and all the values are created and the GamePlay object exists inside the scene hierarchy (screenshot attached) but once the player is spawned the second time and the
    StartGame method is called (invoked by PlayerSpawnedEvent) then all values inside gameComponent are wiped out, it's like somehow gameComponent is cached and still looking to the previous game object that was destroyed the first GamePlayState was accessed. Please let me know if you have any idea why this could be happening. I know it could be hard to understand but I can provide more details if needed.

    Thanks in advance to the community!!
     

    Attached Files:

  2. yoanpumar

    yoanpumar

    Joined:
    Apr 19, 2020
    Posts:
    2
    Hi guys,

    It's me again, I was finally able to fix my problem by myself. I felt like a noob when I found what the problem was but I want to share it with you in case someone has the same dumb problem. I had forgotten to unsubscribe from the Player Spawned event when destroying the instance of the GamePlayState. for that reason the StartGame() method had being called twice (the first time with the destroyed instance). Hope this helps someone else. ;)

    Code (CSharp):
    1.  
    2. // Unsubscribe from player spawn event on GamePlayState exit
    3. CommonData.mainGame.PlayerSpawnedEvent.RemoveListener(StartGame);
    4.  
     
    Last edited: Sep 17, 2020
    Schneider21 likes this.
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Welcome to the forums!

    It's not a coincidence that you figured out the problem shortly after posting it here. Oftentimes, explaining the issue to someone else forces you to approach the problem from a different angle, which may trigger something (or at least lead you down the path to triggering) that makes the solution clear to you.

    That's why there's a mythos in the development world around keeping a rubber duck on your desk. The idea is that having to explain the issue to someone who knows nothing about it will make you more able to see what's actually happening, versus just what you expect to happen.

    Also, kudos for posting your solution even after figuring it out yourself. It's frustrating to find a forum post that has your same issue described with no answer to it, so you've almost certainly helped some future new developer out today.
     
    yoanpumar likes this.