- Home /
How to lock mouse?
 // Use this for initialization
 function Start () {
 }
 void Update()
 {
     if (Input.GetKey(KeyCode.Space))
         Screen.lockCursor = true;
     else
         Screen.lockCursor = false;
 }
 
 // Update is called once per frame
 function Update () {
 }
This doesn't seem to be working. IS there anything wrong with it?
               Comment
              
 
               
              Answer by fafase · Nov 02, 2012 at 02:20 PM
I am not sure what you are trying to do. At the moment, your cursor should get locked only when space is down.
 // Use this for initialization
 function Start () {
 }
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
         Screen.lockCursor = !Screen.lockCursor;
 }
This above will lock/unlock the cursor each time you press space.
Hold on, I copy/pasted your code and realized you are using something wrong. You mix Unityscript and C#.
In Unityscript you use:
 function Update(){}
In C#,
 void Update(){}
Since it asks for namespace, I guess your script is C# so you need to add the whole class declaration
 using UnityEngine;
 using System;
 //There might be other namespace you need here...
 public class ClassName:$$anonymous$$onBehaviour{
     void Update(){}
 }
Your answer
 
 
             Follow this Question
Related Questions
Screen.lockCursor messes my rotation 1 Answer
Locking cursor/mouse on an object 1 Answer
Locking the mouse cursor without it centering after unlocking it? 0 Answers
where should i put this script 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                