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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object reference not set to an instance of an object

Discussion in 'Scripting' started by silaswiesner, Aug 23, 2022.

  1. silaswiesner

    silaswiesner

    Joined:
    Aug 11, 2020
    Posts:
    4
    Hello if i run the Code on Unity i get this error:

    NullReferenceException: Object reference not set to an instance of an object
    MenuManager.OpenMenu (System.String menuName) (at Assets/Scripts/MenuManager.cs:20)
    Launcher.OnJoinedLobby () (at Assets/Scripts/Launcher.cs:24)
    Photon.Realtime.LobbyCallbacksContainer.OnJoinedLobby () (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:4347)
    Photon.Realtime.LoadBalancingClient.OnOperationResponse (ExitGames.Client.Photon.OperationResponse operationResponse) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:2893)
    ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at C:/Dev/photon-sdk-dotnet/PhotonDotNet/PeerBase.cs:872)
    ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at C:/Dev/photon-sdk-dotnet/PhotonDotNet/EnetPeer.cs:583)
    ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at C:/Dev/photon-sdk-dotnet/PhotonDotNet/PhotonPeer.cs:1771)
    Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:226)
    Rethrow as AggregateException: Caught 1 exception(s) in methods called by DispatchIncomingCommands(). Rethrowing first only (see above). (Object reference not set to an instance of an object)
    Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:244)
    Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:145)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MenuManager : MonoBehaviour
    6. {
    7.     public static MenuManager instance;
    8.  
    9.     [SerializeField] Menu[] menus;
    10.  
    11.     private void Awake()
    12.     {
    13.         instance = this;
    14.     }
    15.  
    16.     public void OpenMenu(string menuName)
    17.     {
    18.         for(int i = 0; i < menus.Length; i++)
    19.         {
    20.             if(menus[i].name == menuName)
    21.             {
    22.                 OpenMenu(menus[i].name);
    23.             }
    24.             else if(menus[i].open)
    25.             {
    26.                 CloseMenu(menus[i]);
    27.             }
    28.         }
    29.     }
    30.  
    31.     public void OpenMenu(Menu menu)
    32.     {
    33.         for (int i = 0; i < menus.Length; i++)
    34.         {
    35.             if (menus[i].open)
    36.             {
    37.                 CloseMenu(menus[i]);
    38.             }
    39.         }
    40.         menu.Open();
    41.     }
    42.     public void CloseMenu(Menu menu)
    43.     {
    44.         menu.Close();
    45.     }
    46. }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    menus is an array, correct?

    Menu[] menus;

    But you have

    menus.Open();

    Wouldn't it be menus[index].Open()
     
  3. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    OP has menu.Open() which probably is fine. It appears to be a single instance of the Menu class.

    My initial guess is that menus is uninitialized. Makes sure it's not.

    Also, I'd be inclined to get rid of your CloseMenu() function, menu's have their own Close function so there's no need to have a separate function to do it. Though that's just personal preference. Do what you want ;)
     
  4. silaswiesner

    silaswiesner

    Joined:
    Aug 11, 2020
    Posts:
    4
    Here is the code it should work with.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Menu : MonoBehaviour
    6. {
    7.     public string menuName;
    8.     public bool open;
    9.  
    10.     public void Open()
    11.     {
    12.         open = true;
    13.         gameObject.SetActive(true);
    14.     }
    15.  
    16.     public void Close()
    17.     {
    18.         open = false;
    19.         gameObject.SetActive(false);
    20.     }
    21. }
    22.  
     
  5. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    If you set the Menu object to inactive in the hierarchy then the Menu component on it is also no longer active, hence the error you're getting.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Looks like they edited their post.
     
    Zalosath likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Also, as this is networking, remember there is no in-built mechanism to ensure the menu is still around when your JoinLobby message comes in.

    IOW, if you say join lobby and that menu reference is null, it is up to you to guard that everything in the delegate is still valid at the moment the join callback fires. Or else disconnect the delegate when it isn't. Otherwise you will see this behavior.

    Networking is extremely abstract and advanced and difficult to reason about, so it's important to code defensively and thoroughly.

    It's still the same story however;

    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
    - Identify why it is null
    - Fix that
     
  8. silaswiesner

    silaswiesner

    Joined:
    Aug 11, 2020
    Posts:
    4
    The code should just close the loading menu if its loaded and open the title menu :)
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    It ain't closing no menu that either a) doesn't exist, or b) you don't have a valid reference to.

    Start there. :)
     
  10. silaswiesner

    silaswiesner

    Joined:
    Aug 11, 2020
    Posts:
    4
  11. jl_bat

    jl_bat

    Joined:
    Nov 29, 2021
    Posts:
    1
    hmm, I think is your awake function there, try not to make it private
     
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Unity will call any of the 'magic methods' (Awake, Start, etc) regardless of its protection level.
     
    RadRedPanda likes this.
  13. loganad8_unity

    loganad8_unity

    Joined:
    May 9, 2019
    Posts:
    4
    Hello I am also having this problem we are probably following the same tutorial video
    . I was wondering if OP found a fix