Cursor Lock Code w/ if, else if, and else Statement Errors
Hello! This is my first time posting on the forums, but I'm getting errors and having some problems with if, else if, and else statements. I checked out this site to help, but it's still not working, unfortunately. My goal here is to just get the cursor to freeze to the center of the screen. I'm very new to coding (as I am primarily an artist) so I may have missed something, but I've tried to retain the right number of brackets and to keep them cleanly oriented. Any help would be appreciated!
The two error codes I'm getting are as follows:
Assets/cursorLock.js(16,9): BCE0044: expecting }, found 'else'. Assets/cursorLock.js(19,1): BCE0044: expecting EOF, found '}'.
function Start() {
Cursor.visible = false;
Screen.lockCursor = true;
}
function Update() {
if (Input.OnKeyDown(KeyCode.Escape)); {
Screen.lockCursor = false;
Cursor.visible = true;
}
else if (Input.OnKeyDown(KeyCode.F1)); {
LockCursor();
}
else {
LockCursor();
}
}
function LockCursor() {
Cursor.visible = false;
Screen.lockCursor = true;
Screen.lockCursor = false;
}
Answer by Oribow · Oct 04, 2015 at 03:23 PM
Remove the ";" after each "else", "else if" and "if". Here is your code coreccted:
function Start() {
Cursor.visible = false;
Screen.lockCursor = true;
}
function Update() {
if (Input.OnKeyDown(KeyCode.Escape)) {
Screen.lockCursor = false;
Cursor.visible = true;
}
else if (Input.OnKeyDown(KeyCode.F1)) {
LockCursor();
}
else {
LockCursor();
}
}
function LockCursor() {
Cursor.visible = false;
Screen.lockCursor = true;
Screen.lockCursor = false;
}