- Home /
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?
Answer by aldonaletto · Dec 30, 2011 at 08:59 PM
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!)
Your solution doesn't work. This is the error Unity displays:
NullReferenceException: Object reference not set to an instance of an object TriggerDamage.OnTriggerEnter (UnityEngine.Collider Trigger) (at Assets/$$anonymous$$y Assets/Script/TriggerScripts/TriggerDamage.js:4)
What does this error mean? And maybe you think the players health is connected to the player itself, but it isn't. It's connected to an object called CoreGUI, which I use to kill the player when his health reaches 0.
To summarize everything: The object CoreGUI has the script called HealthScript with the curhealth variable inside. And I have an object called DamagingTrigger with the script called TriggerDamage attached to it. This object also has a box collider attached to it. And I want to lower the curhealth by 10 when the player (just a original 3rd person controller built in), tagged player, touches the collider
Please read this and help me fix the error
Unity is complaining that the variable script is null, and this is happening because HealthScript isn't attached to the player (as you've guessed). To be able to access HealthScript, you could get a reference to the CoreGUI object, like this:
function OnTriggerEnter(Trigger : Collider) { if (Trigger.tag == 'Player') { // get a reference to the CoreGUI object: var coreGui: GameObject = GameObject.Find("CoreGUI"); // get a reference to the script (assu$$anonymous$$g its name is "HealthScript.js"): var script: HealthScript = goreGUI.GetComponent(HealthScript); // access the variable using the reference: script.curhealth -= 10; } }But the easiest solution is what @save suggested: just declare curHealth as a static var in HealthScript.js:
static var curHealth: int = 100;then use it in any script this way:
HealthScript.curHealth -= 10;This will work fine unless you have more than one player, because a static variable is unique - if there were other players, the static var curHealth would be the same for all of them, thus any damage would affect all players, and all of them would die at the same time when curHealth was zeroed.
Answer by save · Dec 30, 2011 at 09:03 PM
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.
Answer by JPB18 · Dec 30, 2011 at 08:55 PM
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 :P