Unity Excpetion

Hi, I wrote this script to destroy a GUI Text field when the right mouse button is clicked and it WORKS but it’s also telling me “Unity Exception. You are not allowed to call this function when declaring a variable. Move it to the line after with a variable declaration.”

And here’s my code:

var them = gameObject;

function Update () {
if (Input.GetButton(“Fire2”))
Destroy (them);
}

I don’t see a problem here, but i’m sure there is one. Any thoughts?

It probably has to do with the fact that when script probably doesn’t have a reference to gameObject until it is attached to the script and is thus complaining. Alternatively you could use.

var them : GameObject;

and just drag your object into the them spot or just call destroy on the gameObject directly.

function Update () {
if (Input.GetButton("Fire2"))
Destroy (gameObject);
}