I am making a fps in C# and I want to have different canvases:
- HUD
- Pop Up Menu
- 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?