x


How do i access a variable in one script from another script?

Hey, I am trying to access a variable in a script called HealthBarScript. HealthBarScript is attached to a gameObject called Player, whilst i want to access in a script called EnemyMovementScript wich is on a gameObject called Enemy. Here are the scripts.

Here is the HeathbarScript

using UnityEngine;
using System.Collections;


public class HealthBarScript : MonoBehaviour {

    public int Maximumhealth = 100;
    public int CurrentHealth = 100;

    public float healthBarLength;

    // Use this for initialization
    void Start () {
       healthBarLength = Screen.width / 2;
    }

    // Update is called once per frame
    void Update () {
       AdjustCurrentHealth(0);
    }

    void OnGUI(){
       GUI.Box(new Rect(10, 10, healthBarLength, 20),
               CurrentHealth + "/" + Maximumhealth); 
    }

    public void AdjustCurrentHealth(int adj){
       CurrentHealth += adj;

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

       if(CurrentHealth > 100)
       {
         CurrentHealth = Maximumhealth;   
       }

       if(Maximumhealth < 1){
         Maximumhealth = 1;   
       }

       healthBarLength = (Screen.width / 2) * (CurrentHealth / (float)Maximumhealth);
    }
}

And here is the MovementScript

var target : Transform; //the enemy's target

var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy

function Awake()

{

    myTransform = transform; //cache transform data for easy access/preformance

}



function Start()

{

    target = GameObject.FindWithTag("Player").transform; //target the player

}



function Update () {

    //rotate to look at the player

    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,

    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);



    //move towards the player

    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}

function OnCollisionEnter(theCollision : Collision){

 if(theCollision.gameObject.name == "Player"){

 }else if(theCollision.gameObject.name == "Golv"){

    Debug.Log("Hit the wall");
 }
}

I want to access CurrentHealth in the EnemyMovementScript at the bottom function called OnCollisionEnter. How would i do that?

What i want to happend is that i want them to collide and the health si withdrawn by like 5 or something.

more ▼

asked Nov 15 '11 at 08:51 AM

Dakk gravatar image

Dakk
1 2 2 2

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

2 answers: sort voted first

Like @syclamoth said, the problem here is the different languages: JS and CS can't see each other during compile time. There are a few ways to solve this, but since you need only a one-way communication path, I think the best alternative is to use SendMessage to call a function in the CS script that modifies the health.
In the CS script:

void AddToHealth(int value){ // add value to the health
    CurrentHealth += value;  // (or subtract, if the value is negative)
}

If both scripts are attached to different objects, you must have in the JS script a reference to the CS object - transform, gameObject, collider etc.
JS script:

var healthObject: Transform; // drag the health bar object here

function DecreaseHealth(){ // this function reduce health by 5
    healthObject.SendMessage("AddToHealth", -5);
}

The other alternatives are:
1- Compiled scripts are visible to any language: if you compile JS after CS, the JS script will be able to "see" the CS one - but not the other way around, thus you may have problems with the JS script being invisible even to other JS code already compiled. The compilation order is explained in Script Compilation.
2- Rewrite the CS script in JS (or vice versa, if you have more CS than JS scripts). Maybe someday Unity will be able to compile JS, CS and Boo with the same compiler, but until that the best solution is to have all scripts written in the same language.

more ▼

answered Nov 15 '11 at 12:04 PM

aldonaletto gravatar image

aldonaletto
41.3k 16 42 195

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

answered Nov 15 '11 at 08:56 AM

DaveA gravatar image

DaveA
26.4k 151 171 256

This... isn't helpful. As far as I can tell, it's a problem with one being in C# and the other being in JS.

Nov 15 '11 at 09:25 AM syclamoth
(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:

x4143
x3722
x3446
x819

asked: Nov 15 '11 at 08:51 AM

Seen: 1993 times

Last Updated: Nov 16 '11 at 04:59 AM