- Home /
How do I create a toggle for showing cursor and then hiding cursor?
Right now I have a button that will hid the cursor just fine but in order to have it appear I have to press the escape key. Here is what I thought would work for creating a toggle.
var toggle: boolean;
function Start () {
Screen.lockCursor = true;
}
function Update () {
if(Input.GetKeyDown("`")){
if(toggle){
Screen.lockCursor = false;
}
else{
Screen.lockCursor = true;
}
}
}
What am I doing wrong here and how do I fix it?
Comment
Best Answer
Answer by mdagreda · Mar 27, 2013 at 08:27 PM
Oh, how dumb I have been. Ok kids remember when you put a boolean in that you want to keep track of a toggle be sure you change it's value when you hit the toggle key. Here is my new code that works. I added a little extra as well.
var toggle: boolean;
function Start () {
Screen.lockCursor = true;
}
function Update () {
if(Input.GetKeyDown("`")){
if(toggle){
Screen.lockCursor = false;
Screen.showCursor = true;
toggle = false;
}
else{
Screen.lockCursor = true;
Screen.showCursor = false;
toggle = true;
}
}
}
Your answer
Follow this Question
Related Questions
One Last GUI Question 1 Answer
Locking cursor Issue - Flash 1 Answer
Unity 3D Screen.lockCursor Problems 2 Answers
Lock the screen 1 Answer
Modifying the pause menu to have screen.lockcursor on at start 1 Answer