Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Passing variables' values between two or more scripts

$
0
0
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 () { }

Viewing all articles
Browse latest Browse all 1875

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>