How to acces variable in other script on other gameboject

Hi All!

I’m trying to make a Checkpoint system for a race game. I got a onGUI countdowntimer script, and each time crossing a checkpoint I want to add a certain amount of seconds to the timer.

This is the script I added on to the checkpoints:

var AddTime : float = 15;

function OnTriggerEnter () {

var script = GetComponent(GameObject.GUIcounter/GUICountDownTimer);
print(script);
print(script.restSeconds);
script.restSeconds =  script.restSeconds + AddTime;
}

Unfortunately, and probably obviously to some of you, this doesn’t work, and returns Null for the script var… What is wrong with this, and how would I get it to work? Í try to acces the script GUICountDownTimer.js and the variable restSeconds, wich is added to the gameobject GUIcounter…

Thanks in advance!!!

To get a reference to a script in another object, you must have a reference to this object. Supposing GUICounter is the object, and GUICountDownTimer.js is the script name, you should write:

var obj: Transform; // <- drag the GUICounter object here

function OnTriggerEnter(){
    var script: GUICountDownTimer = obj.GetComponent(GUICountDownTimer);
    script.restSeconds = script.restSeconds + AddTime;
}