- Home /
Passing variables' values between two or more scripts
I have two scripts that share variables. One is called Hourglass.js and the other is HourOfTheDayCalculator.js.
I have no problem getting HourOfTheDayCalculator to see the variables in Hourglass, but I can't get the variables from HourOfTheDayCalculator to show up in Hourglass. The two variables I am looking at are clockHour and timeOfDayPhrase. The show up in HourOfTheDayCalculator but not Hourglass.
Here are the two scripts, I can't figure this thing out and I've spent almost 2 days trying to get it working!
Hourglass.js
#pragma strict
public class Hourglass extends MonoBehaviour {
/*********************************************/
/* Script Links */
/*********************************************/
public var HourOfTheDayCalculatorScript : HourOfTheDayCalculator;
public var TimeTravelScript : TimeTravel;
/*********************************************/
/* Time Machine On/Off */
/*********************************************/
public var normalPassageOfTime : boolean = true;
var timeTravelActive: boolean;
/*********************************************/
/* Clock Variables */
/*********************************************/
//How many degrees the sun rotates per second
private var degreesOfRotation : float = 360.0 /*degrees*/ / 24.0 /*hours*/ / 60.0 /*minutes*/ / 60.0 /*seconds*/;
//Editor slider variable to adjust speed of time flow
@Range (0.5, 10000.0)
public var speedOfLight : float = 1.0;
//Calculation of time flow speed
@System.NonSerialized
public var speedModifier : float;
//Get Hours, Minutes, and Seconds as a string
@System.NonSerialized
public var currentHour = System.DateTime.Now.ToString("HH");
@System.NonSerialized
public var currentMin = System.DateTime.Now.ToString("mm");
//Convert current number of hours/minutes passed since midnight into usable seconds
private var hoursInSeconds : int = parseInt(currentHour) * 60.0 * 60.0;
private var minutesInSeconds : int = parseInt(currentMin) * 60.0;
//Total seconds passed
@System.NonSerialized
public var timeOfDay : int = hoursInSeconds + minutesInSeconds;
var clockHour : int;
var timeOfDayPhrase;
/*********************************************/
/* Color Variables */
/*********************************************/
public var sun : Light;
public var sunNightColor : Color;
public var sunDayColor : Color;
/*********************************************/
/* Functions */
/*********************************************/
//set position of the sun based on time of day
function setSun()
{
transform.Rotate(0,0,degreesOfRotation * (hoursInSeconds + minutesInSeconds));
//sun.color = Color.Lerp (sunNightColor, sunDayColor, .5 * Time.deltaTime);
}
//rotate the sun based on set speed
function moveSun() {
while (speedOfLight > 0) {
//Multiplies speed variable by number of degrees to rotate per second
speedModifier = degreesOfRotation * speedOfLight;
transform.Rotate(0,0,speedModifier * Time.deltaTime);
yield;
}
}
function normalPassageOfTimeToggle () {
//Setting the normalPassageOfTime to opposite of timeTravelActive
if (timeTravelActive == false){
normalPassageOfTime = true;
} else {
normalPassageOfTime = false;
}
}
function importVariables(){
clockHour = HourOfTheDayCalculatorScript.clockHour;
timeOfDayPhrase = HourOfTheDayCalculatorScript.timeOfDayPhrase;
timeTravelActive = TimeTravelScript.timeTravelActive;
}
}
/*********************************************/
/* **** BEGIN **** */
/*********************************************/
function Awake() {
//Starting normalPassageOfTime as true
normalPassageOfTime = true;
//Linking Scripts
TimeTravelScript = GetComponent("TimeTravel");
HourOfTheDayCalculatorScript = GetComponent("HourOfTheDayCalculator");
importVariables();
}
/*********************************************/
/* Start */
/*********************************************/
function Start () {
normalPassageOfTimeToggle();
if (normalPassageOfTime == true) {
setSun();
moveSun();
}
Debug.Log("TODPhrase within Start function is " + timeOfDayPhrase);
Debug.Log("clockHour within Start function is " + clockHour);
}
/*********************************************/
/* Update Loop */
/*********************************************/
function Update () {
}
//clockHour and timeOfDayPhrase not importing for some reason...
And here's the second one. It's supposed to calculate the clockHour variable (among other things) and send it back to the first script.
HourOfTheDayCalculator.js
#pragma strict
public class HourOfTheDayCalculator extends MonoBehaviour {
/*********************************************/
/* Variables */
/*********************************************/
var HourglassScript : Hourglass;
public var timeOfDay : int;
@System.NonSerialized
var clockHour : int;
@System.NonSerialized
var timeOfDayPhrase;
@System.NonSerialized
var speedModifier : float;
@System.NonSerialized
var normalPassageOfTime;
/*********************************************/
/* Color Variables */
/*********************************************/
public var nightFogColor : Color;
public var duskFogColor : Color;
public var morningFogColor : Color;
public var middayFogColor : Color;
public var nightAmbientColor : Color;
public var duskAmbientColor : Color;
public var morningAmbientColor : Color;
public var middayAmbientColor : Color;
/*********************************************/
/* Functions */
/*********************************************/
function setClockHour (){
while (normalPassageOfTime == true) {
//00:00 - 00:59 (12:00am)
if (timeOfDay <= 3599) {
clockHour = 0;
timeOfDayPhrase = "night";
RenderSettings.ambientLight = nightAmbientColor;
RenderSettings.fogColor = nightFogColor;
}
//01:00 - 01:59
if (timeOfDay >= 3600 && timeOfDay <= 7199){
clockHour = 1;
timeOfDayPhrase = "night";
RenderSettings.ambientLight = nightAmbientColor;
RenderSettings.fogColor = nightFogColor;
}
//02:00 - 02:59
if (timeOfDay >= 7200 && timeOfDay <= 10799){
clockHour = 2;
timeOfDayPhrase = "night";
RenderSettings.ambientLight = nightAmbientColor;
RenderSettings.fogColor = nightFogColor;
}
//03:00 - 03:59
if (timeOfDay >= 10800 && timeOfDay <= 14399){
clockHour = 3;
timeOfDayPhrase = "night";
RenderSettings.ambientLight = nightAmbientColor;
RenderSettings.fogColor = nightFogColor;
}
//04:00 - 04:59
if (timeOfDay >= 14400 && timeOfDay <= 17999){
clockHour = 4;
timeOfDayPhrase = "night";
RenderSettings.ambientLight = nightAmbientColor;
RenderSettings.fogColor = nightFogColor;
}
//05:00 - 05:59
if (timeOfDay >= 18000 && timeOfDay <= 21599){
clockHour = 5;
timeOfDayPhrase = "morning";
RenderSettings.ambientLight = Color.Lerp (nightAmbientColor, duskAmbientColor, speedModifier * Time.deltaTime);
RenderSettings.fogColor = Color.Lerp (nightFogColor, duskFogColor, speedModifier * Time.deltaTime);
}
//06:00 - 06:59
if (timeOfDay >= 21600 && timeOfDay <= 25199){
clockHour = 6;
timeOfDayPhrase = "morning";
RenderSettings.ambientLight = Color.Lerp (nightAmbientColor, duskAmbientColor, speedModifier * Time.deltaTime);
RenderSettings.fogColor = Color.Lerp (nightFogColor, duskFogColor, speedModifier * Time.deltaTime);
}
//07:00 - 07:59
if (timeOfDay >= 25200 && timeOfDay <= 28799){
clockHour = 7;
timeOfDayPhrase = "morning";
RenderSettings.ambientLight = Color.Lerp (duskAmbientColor, morningAmbientColor, speedModifier * Time.deltaTime);
RenderSettings.fogColor = Color.Lerp (duskFogColor, morningFogColor, speedModifier * Time.deltaTime);
}
//08:00 - 08:59
if (timeOfDay >= 28800 && timeOfDay <= 32399){
clockHour = 8;
timeOfDayPhrase = "morning";
RenderSettings.ambientLight = Color.Lerp (duskAmbientColor, morningAmbientColor, speedModifier * Time.deltaTime);
RenderSettings.fogColor = Color.Lerp (duskFogColor, morningFogColor, speedModifier * Time.deltaTime);
}
//09:00 - 09:59
if (timeOfDay >= 32400 && timeOfDay <= 35999){
clockHour = 9;
timeOfDayPhrase = "morning";
RenderSettings.ambientLight = Color.Lerp (duskAmbientColor, morningAmbientColor, speedModifier * Time.deltaTime);
RenderSettings.fogColor = Color.Lerp (duskFogColor, morningFogColor, speedModifier * Time.deltaTime);
}
//10:00 - 10:59
if (timeOfDay >= 36000 && timeOfDay <= 39599){
clockHour = 10;
timeOfDayPhrase = "morning";
RenderSettings.ambientLight = Color.Lerp (morningAmbientColor, middayAmbientColor, speedModifier * Time.deltaTime);
RenderSettings.fogColor = Color.Lerp (morningFogColor, middayFogColor, speedModifier * Time.deltaTime);
//yield;
}
//11:00 - 11:59
if (timeOfDay >= 39600 && timeOfDay <= 43199){
clockHour = 11;
timeOfDayPhrase = "morning";
RenderSettings.ambientLight = middayAmbientColor;
RenderSettings.fogColor = middayFogColor;
}
//12:00 - 12:59
if (timeOfDay >= 43200 && timeOfDay <= 46799){
clockHour = 12;
timeOfDayPhrase = "afternoon";
RenderSettings.ambientLight = middayAmbientColor;
RenderSettings.fogColor = middayFogColor;
}
//13:00 - 13:59 (1:00pm)
if (timeOfDay >= 46800 && timeOfDay <= 50399){
clockHour = 13;
timeOfDayPhrase = "afternoon";
RenderSettings.ambientLight = middayAmbientColor;
RenderSettings.fogColor = middayFogColor;
}
//14:00 - 14:59 (2:00pm)
if (timeOfDay >= 50400 && timeOfDay <= 53999){
clockHour = 14;
timeOfDayPhrase = "afternoon";
RenderSettings.ambientLight = middayAmbientColor;
RenderSettings.fogColor = middayFogColor;
}
//15:00 - 15:59 (3:00pm)
if (timeOfDay >= 54000 && timeOfDay <= 57599){
clockHour = 15;
timeOfDayPhrase = "afternoon";
RenderSettings.ambientLight = middayAmbientColor;
RenderSettings.fogColor = middayFogColor;
}
//16:00 - 16:59 (4:00pm)
if (timeOfDay >= 57600 && timeOfDay <= 61199){
clockHour = 16;
timeOfDayPhrase = "afternoon";
RenderSettings.ambientLight = middayAmbientColor;
RenderSettings.fogColor = middayFogColor;
}
//17:00 - 17:59 (5:00pm)
if (timeOfDay >= 61200 && timeOfDay <= 64799){
clockHour = 17;
timeOfDayPhrase = "evening";
RenderSettings.ambientLight = middayAmbientColor;
RenderSettings.fogColor = middayFogColor;
}
//18:00 - 18:59 (6:00pm)
if (timeOfDay >= 64800 && timeOfDay <= 68399){
clockHour = 18;
timeOfDayPhrase = "evening";
}
//19:00 - 19:59 (7:00pm)
if (timeOfDay >= 68400 && timeOfDay <= 71999){
clockHour = 19;
timeOfDayPhrase = "evening";
}
//20:00 - 20:59 (8:00pm)
if (timeOfDay >= 72000 && timeOfDay <= 75599){
clockHour = 20;
timeOfDayPhrase = "night";
}
//21:00 - 21:59 (9:00pm)
if (timeOfDay >= 75600 && timeOfDay <= 79199){
clockHour = 21;
timeOfDayPhrase = "night";
RenderSettings.ambientLight = nightAmbientColor;
RenderSettings.fogColor = nightFogColor;
}
//22:00 - 22:59 (10:00pm)
if (timeOfDay >= 79200 && timeOfDay <= 82799){
clockHour = 22;
timeOfDayPhrase = "night";
RenderSettings.ambientLight = nightAmbientColor;
RenderSettings.fogColor = nightFogColor;
}
//23:00 - 23:59 (11:00pm)
if (timeOfDay >= 82800 && timeOfDay <= 86399){
clockHour = 23;
timeOfDayPhrase = "night";
RenderSettings.ambientLight = nightAmbientColor;
RenderSettings.fogColor = nightFogColor;
}
yield;
}
}
//Gets the value of timeOfDay from Hourglass script
function getTimeOfDay(){
while (normalPassageOfTime == true){
timeOfDay = HourglassScript.timeOfDay;
yield;
}
}
function importVariables(){
normalPassageOfTime = HourglassScript.normalPassageOfTime;
speedModifier = HourglassScript.speedModifier;
}
}
/*********************************************/
/* **** BEGIN **** */
/*********************************************/
function Awake () {
HourglassScript = GetComponent("Hourglass");
importVariables();
}
function Start () {
getTimeOfDay();
setClockHour();
Debug.Log("HourOfTheDayCalculator : Current TODP: " + timeOfDayPhrase);
Debug.Log("HourOfTheDayCalculator : Current ClockHour: " + clockHour);
}
function Update () {
}
Answer by ScienceIsAwesome · Jul 25, 2015 at 04:05 AM
Ouch, I feel your pain, two days of frustration with something that just won't work.
I moved importVariables(); from Awake to Start in Hourglass and it works now, it's generally not a good idea for scripts to communicate before they have all had a chance to get settled in. In a back and forth situation like this, it's even better to do everything with back and forth function calls, in or after Start.
"Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. Awake is always called before any Start functions."
http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html
Happy coding :)
@ScienceIsAwesome , Thank you so much for taking the time to look through all of that code to come up with a solution, but unfortunately, your solution didn't work for me. It makes sense that it would work better running the importVaribles() function in start, but when I moved it from Awake on Hourglass.js, it still didn't work. my Debug.Logs that I have in the Start should match the ones in HourOfTheDayCalculator.js showing the correct clockHour and timeOfDayPhrase, but ins$$anonymous$$d the clockHour is still showing 0 and the Phrase is blank.
I tried moving the importVariables() from Awake() to Start() on HourOfTheDayCalculator.js to see what would happen, and it actually broke that script too, so I put it back. Any other suggestions?
Thank you again, for looking at my question, perhaps you wouldn't $$anonymous$$d looking again?
Well, when I threw your code into unity, all I had to do was remove three references to the missing TimeTravelScript and make the malfunctioning variables visible in the inspector, I saw them as 0 and blank in Hourglass, moved the function call from Awake to Start and suddenly it worked.
$$anonymous$$y suggestion is using a more robust communication system, you have to make sure the information is ready and accessible. I would create one $$anonymous$$onoBehaviour manager with global variables (or global functions to control access to private variables). Don't exchange information between any classes in Awake, ever, and once all the classes are definitely up and running, they share their information through the manager.
#pragma strict
public class $$anonymous$$anager extends $$anonymous$$onoBehaviour
{
public static var globalInt;
function Awake ()
{
DontDestroyOnLoad(this);
}
}
This globalInt is now accessible from anywhere , if you would like to control access to it you can make it private and use public static functions to manipulate it. To use it in other scripts you use it like this:
$$anonymous$$anager.globalInt = 5;
var i = $$anonymous$$anager.globalInt;
You also have to find some way to synchronize your classes, for example, you want class 1 to write to the variable before class 2 reads it, how do they know that? One of the best ways to accomplish this is with an event system, you can also manipulate the script execution order or simply have the classes update their variables more than once (which is typical). In any case, your problem is one of synchronization or access :)
@ScienceIsAwesome This is fantastic, Thank you! I really appreciate you demonstrating both the manager script as well as how to access it in other scripts.
So with this method, you think it'd be ok to put pretty much all variables that I need to have access to by other scripts in to this manager script?
Thank again!
Yeah, basically variables should be local if only or mostly this one class uses them but when many classes need to use the same information, all of that back and forth nonsense creates problems, just like yours. Global variables are the simplest way to solve many of those problems :)
Of course you could also use functions, class A calls a function in class B which returns the relevant variable by calculating it, that way you know that class A is getting the correct information because class B calculates it on demand
You can use static functions just like static variables, as in ClassName.FunctionName() from anywhere, but yeah, otherwise Class B has to somehow "know" about this instance of Class A