x


Do something when something happens in another script.

I have Script01.js and Script02.js. In Script01.js, I have a variable named "animState". animState goes from 0 - 4 throughout the script, depending on what events take place. Now, I want to be able to tell when animState is a certain value in Script02.js. It would be used like this:

if(script01.animState == 3)
{
//Do something
}

I tried making a new instance of the class:

var script01 : Script01;

and then dragged Script01.js into the var slot of the Script02.js.

I inserted a print into Script02.js to see what's happening:

print(script01.animState);

It's value shows up as 0.

Oh, and the scripts are on different objects.

What am I doing wrong, can anyone help me with this?

Thanks,

Kyle

more ▼

asked Sep 15 '10 at 02:33 PM

Kyle 6 gravatar image

Kyle 6
41 8 8 12

There are a lot of unknowns here. Firstly, can you confirm that your variable is actually changed? Secondly, is your variable static? (if it is, then the first method will work just fine and if not, you will need to get the actual instance attached to the other object, not a new instance unless the value is changed in all instances of the script or something like that).

Sep 15 '10 at 03:01 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Instead of a variable for the script on the GameObject, have a variable for the GameObject to which the script is attached. You can get to the script like this:

var objectWithScript : GameObject; //Set this in the inspector window

...

//Get a reference to the script attached to the GameObject
var scriptO1Ref : Script01 = objectWithScript.getComponent(Script01);

(Forgive me if there are some syntax errors; I usually use C#.) The variable script01Ref should let you manipulate the script you care about.

more ▼

answered Sep 15 '10 at 05:06 PM

HeywoodFloyd gravatar image

HeywoodFloyd
348 7 11 21

(comments are locked)
10|3000 characters needed characters left

You can use both the solutions given here, but I honestly don't see a problem with how you described you did it. It should work.

I think you just have some other error in your code, you dragged the wrong thing in the inspector or you put the print statement in the wrong place. If you want you can upload your project somewhere, and I can try and take a look. Or you can post the code for both scripts here, and a snapshot of the inspectors...

more ▼

answered Oct 19 '10 at 12:08 AM

Cyb3rManiak gravatar image

Cyb3rManiak
1.8k 2 4 18

(comments are locked)
10|3000 characters needed characters left

The post from HeywoodFloyd got it. What I don't see mentioned yet though is the why. When you drag the .js script onto one GameObject and then drag the .js script onto a MonoBehaviour (script2)... these are two instances of this "class". I say class because underneath, Unity compiles all of the javascript files to classes just like C# and puts them in their own assembly. It's not really a "script" at this point so the variables of the script are not shared. The instance of Script1 inside of Script2 (which Unity has created for you) is not getting updated, in fact it's not doing anything so it's state will remain the default.

more ▼

answered Dec 18 '10 at 01:57 PM

Ray Pendergraph gravatar image

Ray Pendergraph
1.7k 16 21 37

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5386
x4382
x1214
x108

asked: Sep 15 '10 at 02:33 PM

Seen: 1518 times

Last Updated: Sep 15 '10 at 02:50 PM