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

Question need a little help figuring out object not found error ( although it's assigned in the inspector)

Discussion in 'Scripting' started by scr33ner, Apr 21, 2023.

  1. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    I set up a canvas, "pauseCanvasBG" as a child of my in game GUI/HUD. Here's the code I'm working with:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.InputSystem;
    6. using UnityEngine.UI;
    7. using UnityEngine;
    8. using TMPro;
    9.  
    10. public class GUI_Input : MonoBehaviour
    11. {
    12.     [HideInInspector] public bool menuMode = false;
    13.     [HideInInspector] public bool gamePaused = false;
    14.     public Canvas pauseMenuCanvasBG;
    15.  
    16.     public void PauseGame()
    17.     {
    18.         if (Keyboard.current.escapeKey.wasPressedThisFrame || Gamepad.current.startButton.wasPressedThisFrame)
    19.         {
    20.             if (!gamePaused)
    21.             {
    22.                 // Pause game
    23.                 Debug.Log("pause");
    24.                 gamePaused = true;
    25.                 menuMode = true;
    26.                 pauseMenuCanvasBG.enabled = true;
    27.                 Debug.Log("Menu mode: " + menuMode);
    28.             }
    29.             else
    30.             {
    31.                 // Resume game
    32.                 Debug.Log("resume");
    33.                 gamePaused = false;
    34.                 menuMode = false;
    35.                 pauseMenuCanvasBG.enabled = false;
    36.                 Debug.Log("Menu mode: "+ menuMode);
    37.             }
    38.         }
    39.     }
    40. }
    pauseMenuCanvasBG in inspector

    Untitled-1.png

    What am I missing?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Well, what is the error and what line does it happen on?

    And have you made sure you don't have another instance of this component somewhere else in the scene?
     
  3. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    error:
    GUI_Input.PauseGame () (at Assets/_06Scripts/GUI_Input.cs:41)

    there's no other instance in the scene.
    PauseGame() is called in the Update() in my RaceManger.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Is that the whole script? There is no line 41 in your script.
     
  5. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    sorry, here's the full script:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.InputSystem;
    6. using UnityEngine.UI;
    7. using UnityEngine;
    8. using TMPro;
    9.  
    10. /*
    11. *
    12. * handle input for GUI
    13. *
    14. */
    15. public class GUI_Input : MonoBehaviour
    16. {
    17.     [HideInInspector] public bool menuMode = false;
    18.     [HideInInspector] public bool gamePaused = false;
    19.     public Canvas pauseMenuCanvasBG;
    20.     public GameObject pauseBG;
    21.     public void Update()
    22.     {
    23.         // use L_Stick like a mouse when paused or in menuMode
    24.         if(menuMode)
    25.         {
    26.             Debug.Log("gamePad");
    27.             GamePad_LStick();
    28.         }
    29.     }
    30.  
    31.     public void PauseGame()
    32.     {
    33.         if (Keyboard.current.escapeKey.wasPressedThisFrame || Gamepad.current.startButton.wasPressedThisFrame)
    34.         {
    35.             if (!gamePaused)
    36.             {
    37.                 // Pause game
    38.                 Debug.Log("pause");
    39.                 gamePaused = true;
    40.                 menuMode = true;
    41.                 pauseMenuCanvasBG.enabled = true;
    42.                 //pauseBG.SetActive(true);
    43.                 Debug.Log("Menu mode: " + menuMode);
    44.             }
    45.             else
    46.             {
    47.                 // Resume game
    48.                 Debug.Log("resume");
    49.                 gamePaused = false;
    50.                 menuMode = false;
    51.                 pauseMenuCanvasBG.enabled = false;
    52.                 //pauseBG.SetActive(false);
    53.                 Debug.Log("Menu mode: "+ menuMode);
    54.             }
    55.         }
    56.     }
    57.     //
    58.     void GamePad_LStick()
    59.     {
    60.         var gamepad = Gamepad.current;
    61.         if (gamepad == null) return; // No gamepad connected.
    62.  
    63.         Vector2 stickInput = gamepad.leftStick.ReadValue();
    64.         Vector2 mouseDelta = new Vector2(stickInput.x, stickInput.y) * 10f;
    65.         Mouse.current.WarpCursorPosition(Mouse.current.position.ReadValue() + mouseDelta);
    66.     }
    67. }
    68.  
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Well a null ref is just a null ref. I guarantee you that you have nother instance of this component lying around in a scene somewhere.

    You can easily locate it by using the second overload of Debug.Log, which takes a Unity object for context and will highlight the component in the heirarchy. You could easily change line 38 to:
    Debug.Log("Pause", this);
    and solve this quickly.
     
  7. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    this is returned:
    Code (CSharp):
    1. pause
    2. UnityEngine.Debug:Log (object,UnityEngine.Object)
    3. GUI_Input:PauseGame () (at Assets/_06Scripts/GUI_Input.cs:38)
    4. RaceManager:Update () (at Assets/_06Scripts/RaceManager.cs:114)
    when using
    Code (CSharp):
    1. Debug.Log("Pause", this);
    I searched my hierarchy for pauseCanvasBG & only one shows up
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Well that's not the component I'm saying you might have multiple of. I'm saying you might have multiple
    GUI_Input
    components. You can type in
    t:GUI_Input
    into the hierarchy search to see which objects have this component.

    Unity doesn't lie about null refs. There's always a simple cause.
     
  9. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    this is the result for
    Code (CSharp):
    1. t:GUI_Input
    Untitled-1.png
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Kurt-Dekker likes this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,768
    I'm always fascinated by how long people will resist doing the first step, the ONLY step that will ever start the process of fixing a NullReferenceException.

    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

    If you THINK you have done Step 1 by finding and printing something that isn't null, you actually haven't done Step 1.

    The simple fact that what you printed was NOT NULL means that you did not find what IS null, so go back and check again. Check a different object. Check what line number you're seeing. REMOVE the script completely, run it again, etc..

    It's really just that simple: you MUST find what is null first. End of story.

    Read what Spiney says above, read the links I posted, there's really nothing anyone can do for you until YOU do Step #1. It's not a global conspiracy against you. It's a nullref.
     
  12. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    187
    Truth is I had no idea how to search for a missing object until yesterday.

    I suppose I'm still confused as to parent/child relationships. The way I implemented everything was fine except for the error- this is what confused me. Ended up fixing it by moving that block of code to the parent.