How do you fix a semicolon error?

I’ve been working for a long time on a JavaScript for an options window, and still can’t get the coding to work. The general idea is that if the variable is 1, the window is open and if the variable is 0, the window is closed. I thought I could do this by creating buttons to change the variable. My question is not on how to code anything new, I just ask for help getting my existing coding to work.

My code looks like:

var myInt : int = 1;

if (myInt<1);
	{
	//This should be where the window is closed
	}

	
if (myInt>1)
	{
	//There should be an error notice here.
	}
	
else

	function () {
	var WindowRect : Rect = Rect (20, 20, 120, 50);
	// Register the window. Notice the 3rd parameter 
	windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
	
	}

	// Make the contents of the window
	function DoMyWindow (windowID : int) {
		if (GUI.Button (Rect (10,20,100,20), "test Button"))
			print ("Got a click");
	//Maybe make a window-closing button here later?
	}

The only error code reads:

Assets/Window.js(21,10): UCE0001: ‘;’
expected. Insert a semicolon at the
end.

When I add the semicolon, 10 new errors show.

Also, please note that I am very new to both Unity and coding in general. Thanks in advance for any help.

I don’t write JavaScript but, to me, it looks like you have several errors in your code.

  • Line 3: if (myInt<1); - that semicolon needs removing
  • Line 5: You need to have something in your if block - not just a comment.
  • Line 11: There needs to be something in your if block - not just a comment.
  • Line 14: The else here means that the following code will get executed if myInt <= 1. You already had a section above that gets executed if myInt < 1… do you really want both?
  • Line 16: You’re trying to define an anonymous function within the else block - I don’t know if Javascript allows this, but it’s certainly not good practice. Give the function a name (i.e. function ThisIsWhatThisFunctionDoes() {), and move it out of the else block.
  • Line 24: Likewise, this function should not be defined within the else block.
  • Line 29: You’re missing a closing }

Actually, there can be comments in the if statements. They don’t necessarily have to do something. The error is in fact that there is a semi-colon in line 3. Also he’s probably looking for a if,else if, else, structure so on line 9 make it else if instead of just if. You also want to surround lines 15-28 with curly braces so that all those lines of code get executed in the else. Lastly, when you want to call a function you have to make them first and then call them. Here is what your code should look like.

var myInt : int = 1;
if (myInt<1)     
{     
     //This should be where the window is closed     
}       
else if (myInt>1)     
{     
     //There should be an error notice here.     
}      
else{
     windowFunction();//this calls the 'windowFunction' function
     DoMyWindow(10)//this calls the 'DoMyWindow' function
}      
function windowFunction () {     
     var WindowRect : Rect = Rect (20, 20, 120, 50);
     //Register the window. Notice the 3rd parameter      
     windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window");
          }      
// Make the contents of the window     
function DoMyWindow (windowID : int) {         
     if (GUI.Button (Rect (10,20,100,20), "test Button"))
          print ("Got a click");     
     //Maybe make a window-closing button here later?
}