- Home /
 
What am I doing wrong in this
I am a beginner and I'm trying to write a code to hide the cursor and lock it in place, but when I test it the cursor is hidden but not locked in place, please help
here is my code
 #pragma strict
 
 function Start () {
     Screen.showCursor = false;
     Screen.lockCursor = true;
 }
 
 function DidShowCursor(){
     Debug.Log("Showing cursor");
 }
 
 function DidHideCursor(){
     Debug.Log("Hiding cursor");
 }    
 
 function DidUnlockCursor(){
     Debug.Log("Unlocking cursor");
     guiTexture.enabled = true;
 }
 
 function DidLockCursor(){
     Debug.Log("Locking cursor");
     guiTexture.enabled = false;
 }    
 
 function OnMouseDown (){
     //lock the cursor
     Screen.lockCursor = true;
     //hide the cursor
     Screen.showCursor = false;
 }    
 
 private var wasLocked = false;
 private var wasShown = true;
 
 function Update () {
     //if escape is pressed, unlock cursor and show cursor
     if (Input.GetButtonDown("Esc"))
         Screen.showCursor = true;
         Screen.lockCursor = false;
         
     if (!Screen.showCursor && wasShown){
         wasShown = true;
         DidShowCursor();
     }
     
     else if (Screen.showCursor && !wasShown){    
         wasShown = false;
         DidHideCursor();    
     }        
     
     if (!Screen.lockCursor && wasLocked){
         wasLocked = false;
         DidUnlockCursor();
     }        
     
     else if (Screen.lockCursor && !wasLocked) {
         wasLocked = true;
         DidLockCursor();
     }
 }    
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by craigaw · Jan 17, 2014 at 03:30 AM
I didn't actually test your code, but by the looks of things you are missing some brackets on your first if statement, so that the cursor is always unlocked. It should be,
 if (Input.GetButtonDown("Esc")) {
    Screen.showCursor = true;
    Screen.lockCursor = false;
 }
 
              Yep, fair enough. It's generally why a lot of people don't like dropping the braces on statements like that. It can be a little dangerous, because it won't throw an error and can be hard to trace.
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Unity jumping sound, help. 1 Answer
Cursor Locks/Hides but Will Not Reappear? 1 Answer
One Last GUI Question 1 Answer