Changing variables in another script

Hi everybody

I’ve made a health script with health bars and a current health variable. It works fine, but now I want to change this current health variable when you touch a trigger. In the script of this trigger I want to say that if the player collides with the trigger, his current health will lower by 10. But I can’t get the variable from the script. I’ve read many things about .GetComponent on the internet, but I still don’t get it. Do I need to use .GetComponent to change the current health? Here’s my health script:

var maxhealth = 100;
var curhealth = 100;
var controlTexture : Texture2D;  
var soundAlmostdead : AudioClip;
var chainsawActive : boolean = false; 

function Update () {

if (curhealth < maxhealth/10){
audio.PlayOneShot(soundAlmostdead);
}

if (curhealth == 0 && chainsawActive == false) {
transform.Translate(0,40,0);
chainsawActive = true;
}

if (curhealth < 0) {
curhealth = 0;
}

if (curhealth > maxhealth) {
curhealth = maxhealth;
}
}

function OnGUI () {

    if(maxhealth >= 0){
GUI.Button(Rect (0,0,(10*maxhealth),26), maxhealth.ToString(), "box");
    }
    if(curhealth >= 0){
GUI.Button (Rect (0,1,(10*curhealth),24), curhealth.ToString(), "box");
    }
}

Yes, I know, I use a strange way of killing the player when his health reaches 0. This script is attached to an object called GUICore. (which also has a trigger attached to it to kill the player.) And this is the trigger script, attached to an object called DamagingTrigger:

function OnTriggerEnter(Trigger : Collider) {
    if (Trigger.tag == 'Player') {

And here I want to lower the health of the player by 10;
}
}

How can I solve this problem?

You must get a reference to the player, what in this case may be the Trigger variable passed to the event, and get its script with GetComponent(ScriptType), where ScriptType is the script name without quotes or extension:

function OnTriggerEnter(Trigger : Collider) {
  if (Trigger.tag == 'Player') {
    // get a reference to the script (assuming its name is "HealthScript.js"):
    var script: HealthScript = Trigger.GetComponent(HealthScript);
    // access the variable using the reference:
    script.curhealth -= 10;
  }
}

Remember to use the actual name of your health script instead of HealthScript (unless it’s called HealthScript, of course!)

The player's health could probably be a static variable so then you'd just have to call:

static var curHealth : int = 100;

function OnTriggerEnter(other : Collider) {
    if (!other.CompareTag("Player")) return;
    PlayerScript.curHealth -= 10;
}

or

function OnTriggerEnter(other : Collider) {
    if (!other.CompareTag("Player")) return;
    other.GetComponent(PlayerScript).curHealth -= 10;
}

Remember that `OnTriggerEnter (variable : Collider)` refers to the entering object, so to not confuse yourself, it's best to name it "other" as it is the other object intersecting with the collider.

If you where using C# you could call the variable by defining the script other script name I guess (I guess, I used to do this on Visual Studio with windows forms, don’t know if it’s possible to do that in Unity).

But yes i’d guess you’d have to use the .GetComponent:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

Hope I’ve helped :stuck_out_tongue: