- Home /
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:
//player.js
var object : GameObject = hit.collider.gameObject;
var myAI : MyAI = object.GetComponent(MyAI);
if(Input.GetKey("1"))
{
Debug.Log ( "Your asnwer is : A" );
myAI1.ANSWER = "A";
}
//myAI.js
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?
thnx!
Answer by aldonaletto · Jun 24, 2011 at 11:27 AM
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.
Your answer
Follow this Question
Related Questions
How to make a N-S magnet Javascript? 1 Answer
How do I make a sword swing? 2 Answers
{Easy question} How to disable script from other gameObject. 2 Answers