Referencing a variable in another script from a script

I lost where I’d seen this … how to reference a variable in script a when you are running script b.

In my start script I have an awake function.

When I click a button that has playagain.js I need it to set Time.time back to zero. Time.time is in the timer.js script and runs a clock down. Time.time I think runs from the start of the game or on loadlevel? So I put something in the awake function or maybe write a new start function (after thew awake) in my start script that will do that.

I’m still a beginner but maybe I can be of some help.

If you want to change a variable in another script you can do something like this.

//you will select the object that the script is running on in the inspector
var other : nameOfScript;

function Update (){
   other.nameOfVariable = 1;
}

I believe you can also check the variable in the external script by using something like this.

var other : nameOfScript;

function Update (){
   if (other.nameOfVariable == 1){
      // your stuff here
   }
}

Does this help?

DaveyJJ,
I was trying to do your “simple unity tutorial” and reset the time as well… (if this is what you are doing) you may want to check out On The Edge - Det bedste fra tech verdenen and see if this will help what you are trying to do.

Let me know how it goes as I would like to reset the time as well :wink:

Isn’t working. Basically, I need to reset Time.time back to 0 when the Application.LevelLoad goes off. Working on it now, thanks CD.

Not sure you can set Time.time to zero. If not, just save it’s value (when you would normally try to reset it) and subtract that value from any “time” you might need.

Richard.

Resurrecting this thread.

I’d like to reference an instance of a script that is attached to an object when another object collides with it. I’m working in C#, and having some trouble. I’ve seen ways to do this with Javascript, but I don’t want to re-write in javascript.

Here is my code. The problem is on line 20. I can’t seem to find the right declaration for the script instance. Any ideas?

Component beltScript = (ConveyorBelt)currentBelt.GetComponent(ConveyorBelt);

Hint: I’ve introduced another error in this line… you find it, and fix it (or the rest of the script) :wink:

d.

Here’s an example from my code:

GUIText txt = (GUIText)txtLabel.GetComponent(typeof(GUIText));

Here I’m getting a GUIText component, not a script, but the principle is the same.

Bampf, the typeof() function (or whatever it is) solved the problem, thanks so much!

David, I don’t know what you’re implying, but it sounded like you were saying that my whole script is broken!