- Home /
(C#) Screen.lockCursor not working?
Hello Unity members. This is my first time using Screen.LockCursor so this maybe a noob question. Anyways the proble I am getting it that when the cursor gets unlocked from another script like my PauseMenu script it will not lock agian once I exit the PauseMenu. All the script looks fine to me but I cant seem to get it to work.Help would be nice. :D
Edit I forgot to say this at the beggining but the cursor is locked when you press play
CursorControl Script
using UnityEngine;
using System.Collections;
public class CursorControl : MonoBehaviour
{
//Get scripts Here
private Inventory inventory;
private PauseMenu paused;
// Use this for initialization
void Start ()
{
//Get Scripts here
inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
paused = GameObject.FindGameObjectWithTag("GUI Manager").GetComponent<PauseMenu>();
Screen.lockCursor = true;
}
// Update is called once per frame
void Update ()
{
//Unlock the Cursor
if(inventory.inventoryIsShown == true || paused.lockCursor == true)
{
Screen.lockCursor = false;
}
//Unlock the Cursor
if(inventory.inventoryIsShown == false && paused.lockCursor == false)
{
Screen.lockCursor = true;
}
}
}
PauseMenu Script
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]//Be seen in edit mode
public class PauseMenu : MonoBehaviour
{
//Public
public bool isPaused;
public bool lockCursor;
//Private
private bool ShowPauseMenu;
private bool ShowOptions;
private bool ShowSettings;
private bool ShowQuit;
//Center Window
public Rect WindowPosition = new Rect(20,20, 120, 50);
public bool CenterWindow;
private Rect CurrentRect = new Rect(20,20, 120, 50);
// Use this for initialization
void Start ()
{
CenterWindow = true;
isPaused = false;
lockCursor = false;
ShowPauseMenu = false;
ShowOptions = false;
ShowSettings = false;
ShowQuit = false;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
ShowPauseMenu = !ShowPauseMenu;
isPaused = !isPaused;
lockCursor = !lockCursor;
}
}
void OnGUI ()
{
if(CenterWindow)
{
CurrentRect.x = (Screen.width * 0.5f) - (CurrentRect.width * 0.5f);
CurrentRect.y = (Screen.height * 0.5f) - (CurrentRect.height * 0.5f);
}
if(ShowPauseMenu)
{
CurrentRect = GUILayout.Window(0, CurrentRect, PauseMenuWindow, "Pause Menu",GUILayout.MinHeight(Screen.height * 0.25f),GUILayout.MinWidth(Screen.width * 0.25f));
}
if(ShowOptions)
{
CurrentRect = GUILayout.Window(1, CurrentRect, OptionsWindow, "Options",GUILayout.MinHeight(Screen.height * 0.25f),GUILayout.MinWidth(Screen.width * 0.25f));
}
}
void PauseMenuWindow(int windowID)
{
GUILayout.BeginVertical();
if(GUILayout.Button("Resume Game"))
{
Debug.Log ("Resume Game");
ShowPauseMenu = false;
isPaused = false;
}
GUILayout.Space(2);
if(GUILayout.Button("Options"))
{
Debug.Log ("Options");
ShowPauseMenu = false;
ShowOptions = true;
}
GUILayout.Space(2);
if(GUILayout.Button("Save and Quit"))
{
Debug.Log ("Save and Quit");
ShowPauseMenu = false;
ShowQuit = true;
}
GUILayout.EndVertical();
}
void OptionsWindow(int windowID)
{
GUILayout.BeginVertical();
if(GUILayout.Button("Settings"))
{
Debug.Log ("Settings");
ShowOptions = false;
ShowSettings = true;
}
GUILayout.Space(2);
if(GUILayout.Button("Back"))
{
Debug.Log ("Back");
ShowOptions = false;
ShowPauseMenu = true;
}
GUILayout.EndVertical();
}
}
Thanks for your help :D
Should look like this(Tell me if it still does not work.):
if(inventory.inventoryIsShown == false || paused.lockCursor == false)
{
Screen.lockCursor = true;
}
Thanks for the fast response :D
No this does not work...I think that that is the first thing that I tried to try to make this script work but it didn't work... This is one of the main reasons why I am confused.
And if you want to know why I did && ins$$anonymous$$d of || if is because they both need to be false for the cursor to lock.
One thing that I just tried is....
void Update()
{
Screen.lockCursor = true;
//Unlock the Cursor
if(inventory.inventoryIsShown == true || paused.lockCursor == true)
{
Screen.lockCursor = false;
}
//lock the Cursor
if(inventory.inventoryIsShown == false || paused.lockCursor == false)
{
Screen.lockCursor = true;
}
and that doesn't even lock the cursor.
Ok I think I might have found out what is happening. The when you play in the editor the screen.lockCursor is buggy so it doesnt work 100%.... So I tried it on the build and now the cursor can be seen when the game is paused but it still cant be moved....
I am totally stuck..