- Home /
How do I check teh value of a variable in another script.
I need to check the number in another variable. Not add to it or take away but simply check what it is.
For example:
// ScriptA.js
var health = 100;
// ScriptB.js
var healthFromScriptA : int = what ever is in script A;
function Update()
{
print(healthFromScriptA)
}
Answer by aldonaletto · Oct 12, 2013 at 04:37 PM
This is the most frequent question in UA. Take a look at this answer.
I made it kind of work. When I use this script it gives me an error.
#pragma strict
private var survivalScript : GUISurvival;
var targetObj: Transform;
var foodLevel : int;
function Start ()
{
survivalScript = GameObject.Find("Player").GetComponent(GUISurvival);
var targetScript: GUISurvival = targetObj.GetComponent(GUISurvival);
}
function Update()
{
foodLevel = targetScript.currentHunger;
}
function OnTriggerEnter (Col : Collider)
{
if(Col.tag == "Player")
{
Destroy(gameObject);
survivalScript.currentHunger += 10;
}
}
Error: (15,21): BCE0005: $$anonymous$$ identifier: 'targetScript'.
It's almost ok - you've changed the script reference variable to survivalScript, but still are using targetScript. Replace targetScript by survivalScript and remove the second line of Start:
#pragma strict
private var survivalScript : GUISurvival;
var foodLevel : int;
function Start ()
{
survivalScript = GameObject.Find("Player").GetComponent(GUISurvival);
}
function Update()
{
foodLevel = survivalScript.currentHunger;
}
function OnTriggerEnter (Col : Collider)
{
if(Col.tag == "Player")
{
Destroy(gameObject);
survivalScript.currentHunger += 10;
}
}
Your answer
Follow this Question
Related Questions
Using variable from one script attached to one object in another script attached to another object 0 Answers
How could you access a script of varying name? 5 Answers
Link 2 variables from 2 different scripts to 3rd script 1 Answer
Unity mathematics phrasing. 1 Answer
functions file and NullReferenceException: Object reference not set to an instance of an object 1 Answer