- Home /
Get valor of variable in another script
Hi, I have two scripts, health.js
var health = 100f;
var enemy : GameObject;
function OnTriggerEnter (bullet : Collider)
{
Debug.Log(health);
if (bullet.gameObject.CompareTag("bullets")){
vida -=1;
Destroy(Bala.gameObject);
if (vida <=0)
{
Destroy(Enemigo,0f);
}
}
}
and hpbar.js for health bar.
var target : Transform;
function Update ()
{
var wantedPos = Camera.main.WorldToViewportPoint (target.position);
transform.position = wantedPos;
}
And I need valor of health variable in health.js on hpbar.js for decrease the health bar. Help pls! :)
Answer by OrbitSoft · Jan 03, 2014 at 12:52 PM
the health variable needs to be static but you won't see it anymore in the inspector:
static var health : float = 100;
Your script name is "health" and the variable name is health too, which is a bad thing, rename the script to something else, like "Player.js" or whatever, then acces the variable like this:
someVar = Player.health;
Your answer
![](https://koobas.hobune.stream/wayback/20220613125429im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NullReferenceException : Object reference not set to an instance of an object 0 Answers
Calling variables from other scripts 2 Answers
problems accessing variables and functions in another script 1 Answer
Access other script from Editor script 2 Answers
How to link this 2 variables from 2 different scripts / gameobjects? 1 Answer