How do I switch Canvases? & NullReferenceException (U5)

I am making a fps in C# and I want to have different canvases:

  1. HUD
  2. Pop Up Menu
  3. Pop Up Settings Menu

This is part of the ui script in a map:

using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
using System.Collections;
using System.IO;

public class userInterface : MonoBehaviour
{
    public Canvas hud;
    public Canvas popUpMenu;
    public Canvas settingsMenu;

    void Start()
    {
        hud = hud.GetComponent<Canvas>();
        popUpMenu = popUpMenu.GetComponent<Canvas>();
        settingsMenu = settingsMenu.GetComponent<Canvas>();

        hud.enabled = true;
        popUpMenu.enabled = false;
        settingsMenu.enabled = false;
        Cursor.visible = false;
    }
}

But then I get this error:

‘Assets/Scripts/userInterface.cs(81,9): error CS0120: An object reference is required to access non-static member: userInterface.hud’

And this one:

‘Assets/Scripts/userInterface.cs(82,9): error CS0120: An object reference is required to access non-static member userInterface.popUpMenu’

And this one:

‘Assets/Scripts/userInterface.cs(83,9): error CS0120: An object reference is required to access non-static member userInterface.settingsMenu’

So I changed:

public Canvas hud; 
public Canvas popUpMenu;
public Canvas settingsMenu;

Into:

public static Canvas hud;
public static Canvas popUpMenu;
public static Canvas settingsMenu;

But then I get this error:

‘NullReferenceException: Object reference not set to an instance of an object userInterface.Start () (at Assets/Scripts/userInterface.cs:19)’

And this one:

‘NullReferenceException: Object reference not set to an instance of an object userInterface.Update () (at Assets/Scripts/userInterface.cs:31)’

And this one:

‘NullReferenceException: Object reference not set to an instance of an object userInterface.Update () (at Assets/Scripts/userInterface.cs:31)’

But I can’t assign the canvases to the variables, because static objects don’t show up in the inspector.
And when the variables were only public they didn’t show up in the inspector either.

I want to switch canvases.

What do I do now?

NullReferenceException means you didn’t put anything in the inspector view. so if it didn’t show up in the inspector using “public Canvas hud”. its probably bugged. so restart your unity. look at the object that contains the script. check your inspector again.

You probably only need one canvas. Each of your UI elements should be attached to a child of the canvas you want them drawn on; If you want to switch those UI elements on and off, just enable and disable their objects. You can group several UI elements under an empty game object to control them as one.

If your userInterface script was attached to the same object as the canvas component, you would write hud = GetComponent<Canvas>(); to get a reference to it. This is shorthand for hud = this.GetComponent<Canvas>();, where the this keyword refers to the instance of your userInterface class. The error messages are a result of you calling hud.GetComponent instead.

Honestly it sounds like you’re in a little over your head and you need to do some research first about GameObjects, Components, and organizing a scene in Unity, as well as the static keyword in C#. Out of scope for this question, but Google around and try out some of the tutorials on Unity’s site and YouTube. Read the manual! Unity - Manual: Unity User Manual 2021.3 (LTS)

Hi,

the Fast and easy way to switch in multiple is some thing like the following script:

using UnityEngine;
using System.Collections;

public class UserInterface : MonoBehaviour
{

	public enum CanvasStates {
		Hud,
		PopUp,
		Setting,
	}

	internal CanvasStates canvasState;

	[Header("Canvas")]
	[ToolTip("Place the hud canvas here")]
	public GameObject hud;

	[ToolTip("Place the popUpMenu canvas here")]
	public GameObject popUpMenu;

	[ToolTip("Place the settingsMenu canvas here")]
	public GameObject settingsMenu;


	// This method will remain active one cavas at a time just pass the state like
	/*Example
	public void Example(){
		SetCanvasState (CanvasStates.PopUp);
	}
	*/
	public void SetCanvasState(CanvasStates state){
		hud.SetActive (state == CanvasStates.Hud);
		popUpMenu.SetActive (state == CanvasStates.PopUp);
		settingsMenu.SetActive (state == CanvasStates.Setting);
	}

}

Please let me know if you face any difficulty regarding this. I havn’t tested the script.

I found a solution on my own:

I changed:

public Canvas hud;
public Canvas popUpMenu;
public Canvas settingsMenu;

Into:

public GameObject hud;
public GameObject popUpMenu;
public GameObject settingsMenu;

I removed the GetComponent lines.

And replaced:

hud.enabled = true;
popUpMenu = false;
settingsMenu = false;

Into:

hud.SetActive(true);
popUpMenu.SetActive(false);
settingsMenu.SetActive(false);

It worked completely fine.

Thanks for all your help though!