Exception error

I’m sure I’m missing something really basic here but I’m getting the following error when I stop running the game.

UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don’t use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
UnityEngine.GameObject.FindWithTag (System.String tag)
play_slot…ctor () (at Assets/Standard Assets/Scripts/play_slot.js:1)

The script in question is this:

var slot = gameObject.FindWithTag("slot");


function Update () {
	  if (Input.GetKeyUp ("space")) {
        print ("space key was released");
        slot.animation.Play("play");
        
    }
}

It sounds like I’m being told to write it like this:
var slot : gameObject;
slot = gameObject.FindWithTag(“slot”);

But then the variable disappears from the inspector, the game won’t run at all, and more error messages ensue.

Any help would be appreciated.

/Mark

Classes always start with upper case.

So GameObject
gameObject is the property within a transform which will return this transforms linked game object.
But none of that solves your issue. You can not call a function on declaration

or completely

var slot : GameObject;


Function Awake() {
 slot = gameObject.FindWithTag("slot");
}

Ah, OK. Got that but… I changed the line to match your example, and it generates the same error.

BTW, is there a decent scripting primer anywhere? I’ve read on the boards, that books on Javascript are no help, because Unity JS is different.

/Mark