- Home /
Duplicate Question
Missing Documentation on accessing Variables
The following link is down: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
I have been having issues accessing a variable from one script on a GUI text to a second script attached to a another GUI text.
My searches keep pointing me to this dead link. This is a pretty basic issue. I would like to do it in Javascript.
here is my code:
Script 1: Timer.js - On first GUI Text
function Update () {
var gameTime = Time.time;
var playerDays : float = 0;
guiText.text = gameTime.ToString();
if(gameTime > 30)
{
playerDays ++;
guiText.text = "A day in game time";
}
}
Script 2: Player Days.js - On Second GUI Text
function Update () {
playaDays = Timer.playerDays;
guiText.text = playaDays.ToString();
}
I would like to get the value for playerDays in Timer.js in Player Days.js Thank you for any help
The documentation for that isn't missing as it doesn't belong in the Unity docs. It belongs in the Javascript docs.
No it doesn't. For one thing, Unity doesn't use Javascript...it's Unityscript, and web Javascript docs are mostly useless for that. Also it's specific to Unity since it involves using GetComponent. The correct link is http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Ah you're totally right @Eric5h5 - I should refrain from answers at 5am lol.
Answer by fendorio · Apr 17, 2014 at 05:29 AM
Does the word 'scope' sound familiar at all?
At the minute PlayaDays is within the scope of the Update function - it doesn't exist outside of the function.
To make it accessible from another script you need to define it in the 'global scope'
var playaDays : float = 0;
function Update()
{
var gameTime = Time.time;
guiText.text = gameTime.ToString();
if(gameTime > 30)
{
playerDays ++;
guiText.text = "A day in game time";
}
}
With your code as-is, playerDays shouldn't ever not be 0 because at the start of every frame you're assigning 0 to it.
Then from your other script, use GetComponent().playaDays.