Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Infinite_Gamer · Apr 03, 2014 at 10:12 PM · lockcursorscreen.lockcursor

(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
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HuskyPanda213 · Apr 04, 2014 at 01:02 AM 0
Share

Should look like this(Tell me if it still does not work.):

 if(inventory.inventoryIsShown == false || paused.lockCursor == false)
        {
          Screen.lockCursor = true;
        }
avatar image Infinite_Gamer · Apr 04, 2014 at 02:17 AM 0
Share

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.

avatar image Infinite_Gamer · Apr 04, 2014 at 02:28 AM 0
Share

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.

avatar image Infinite_Gamer · Apr 04, 2014 at 03:16 AM 0
Share

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..

avatar image Fikzy · Aug 12, 2014 at 07:16 PM 0
Share

Still no answers ? I'm stuck with this problem too !

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cursor.LockState = CursorLockMode.Locked doesn‘t works。 0 Answers

Cursor.lockState not doing anything 0 Answers

Get change in mouse position while locked 0 Answers

Trouble with Unity 4.6.1f1 Screen.lockCursor 1 Answer

How can i lock the mouse cursor in the middle of the screen ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges