How to fix pause menu bug?

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.

I’m not seeing m,y console log so ill try doing what you said, if it works ill reply again.

I have two new scripts.

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!”

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

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

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

http://plbm.com/?p=221

NOTE: this type of code:

is exactly what I mean in the link above:

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.

The longer your lines of code are, the harder they will be for you to understand them.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

Thank you so much this help a ton! The bug was I didn’t set my cursor controller to have the tag “cursorcontroller” so it wasn’t finding anything.

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!");
        }
    }
}

}