I’m trying to make a pause menu using a ui canvas, whenever I try to use the “V” key to open it it does nothing.
Here’s my current script.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEngine.UIElements;
using UnityEditor.SearchService;
public class pause : MonoBehaviour
{
CursorController cursorcontroller;
public void Start()
{
cursorcontroller=GameObject.FindGameObjectWithTag("cursorcontroller").GetComponent<CursorController>();
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
Debug.Log("Opened menu!");
cursorcontroller.ShowMouseCursor();
gameObject.SetActive(true);
}
}
public void activateMenu()
{
gameObject.SetActive(true);
Debug.Log("Opened menu!");
cursorcontroller.ShowMouseCursor();
}
}
Do you see the log message in the console? That’s always the first thing to check, does the code actually get executed? Also put a log message before the if statement, to make sure it’s called as well.
It looks like you’re (de-)activating the game object the pause script is on? In which case it won’t work, because Update() is not called for scripts on inactive game objects. You’ll need to put the script opening the menu somewhere outside of the menu hierarchy, so that it stays active even when the menu is closed.
This one on a game object called “PauseMenu” with the tag “pausemenu”:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEngine.UIElements;
public class pause : MonoBehaviour
{
public void Start()
{
gameObject.SetActive(false);
}
}
And this one on a game object called “pausemenucontroller” with the tag “pausemenucontroller”
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEngine.UIElements;
public class pausegame : MonoBehaviour
{
CursorController cursorcontroller;
GameObject menu;
public void Start()
{
cursorcontroller=GameObject.FindGameObjectWithTag("cursorcontroller").GetComponent<CursorController>();
menu = GameObject.FindGameObjectWithTag("pausemenu");
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
Debug.Log("Opened menu!");
cursorcontroller.ShowMouseCursor();
menu.SetActive(true);
}
}
}
the menu still doesnt wwork and give me the error
"NullReferenceException: Object reference not set to an instance of an object pausegame.Update () (at Assets/pausegame.cs:22)
"
the script is being run because i’m getting a console message saying “Opened menu!”
Here is the modified version of your script:
using UnityEngine;
public class PauseMenu : MonoBehaviour
{
public GameObject pauseMenuCanvas; // Drag your canvas object here in the inspector
private CursorController cursorController;
void Start()
{
cursorController = GameObject.FindGameObjectWithTag("cursorcontroller").GetComponent<CursorController>();
// Ensure that the pause menu starts as inactive
if (pauseMenuCanvas != null)
{
pauseMenuCanvas.SetActive(false);
}
}
void Update()
{
// Check for key press to open the pause menu
if (Input.GetKeyDown(KeyCode.V))
{
Debug.Log("Opened menu!");
// Show mouse cursor if the cursorController is properly set
if (cursorController != null)
{
cursorController.ShowMouseCursor();
}
else
{
Debug.LogWarning("CursorController not found!");
}
// Activate the pause menu canvas
if (pauseMenuCanvas != null)
{
pauseMenuCanvas.SetActive(true);
}
else
{
Debug.LogWarning("Pause menu canvas is not assigned!");
}
}
}