Bracket error

I am trying to use numbers in my script but i can’t do it because Unity show those errors:


Why? This is so strange! Why does a simple number bug the script? This doesn’t make any sense…

Here is the code that gives the problem (Javascript):

function Update ()
{
    if (Input.GetKeyDown ("1"))
        
}

"The Power of a Number". Best title!

1 Answer

1

Your If statement isn’t doing anything, and the way you’re looking up what key is pressed isn’t right. You need something like:

function Update ()
 {
     if (Input.GetKeyDown (KeyCode.Alpha1))
      {
            //Do stuff in here.....
      }
 }

Actually, using the string input "1" will work (though you're right to say it's bad practice - https://docs.unity3d.com/Manual/ConventionalGameInput.html). The reason for the error is the misformed structure of the if statement.