Setting the cursor to be locked when a key is hit on keyboard
When you're in the game and hit the z button, it pops up the pause menu and a darkening background. The problem is that the cursor is disabled and locked while in game but i want it to be enabled and unlocked when they hit the pause button and then lock it when they resume the game. I've tried Cursor.LockState = CursorLockMode.None and Cursor.visible = true. But it doesn't seem to work. Below is the code for hitting the pause button. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PauseGame : MonoBehaviour {
public Transform pausebackground;
public Transform pausemenu;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Z))
{
if (pausebackground.gameObject.activeInHierarchy == false && pausemenu.gameObject.activeInHierarchy == false)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
pausebackground.gameObject.SetActive(true);
pausemenu.gameObject.SetActive(true);
}else
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
pausebackground.gameObject.SetActive(false);
pausemenu.gameObject.SetActive(false);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612111247im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Programmmed Animation (making transform move to vectors with code) 0 Answers
Mouse-Keyboard Not Working - New User 1 Answer
Trying to program two buttons to appear when the player in my game dies 0 Answers
Unity Annoyance with Physics Help C# Scripting Problem Momentum Issue Please Help 0 Answers
My script to move a gameobject to the closest tagged gameobject doesn't work 1 Answer