Changing a script value

Hello! Well, I need to send a var throw one script to other

I tried it to do it like this:


var object : GameObject = hit.collider.gameObject;

var myAI : MyAI = object.GetComponent(MyAI);



    Debug.Log ( "Your asnwer is : A" );
    myAI1.ANSWER = "A";



var question : String;

static var ANSWER : String;

function Update ()


if(ANSWER == question)


	Destroy (gameObject,0);



It works but my problem is that, when there are more than one object with the same answer, when you answer it right all the objects are destroyed and not only the one that you are selecting.

I guess is because with “GetComponent” im modifying the main script MyAI.js, and not the values of the object attached to it…

So when I have many objects that are taking main script as a reference, how can I change the values of each objects without changing the main script?


It’s because you have declared ANSWER as static. A static variable is created once, and all scripts access the same variable. Just remove the static word and it should work fine, since you are already getting the script instance of the object hit.

EDITED: When an object is instantiated, everything attached to it is instantiated too, including scripts and their non-static variables, so you can modify a variable in one script without altering other instances of the same variable.