Referencing Variables between scripts?

While writing a day and night cycle I decided to include some temperature shifting depending on the time of day. After completion I realized that it’d be better to separate a temperature script and day/night cycle script. This is an example of what I have:

		if(_timeOfDay > sunRise  _timeOfDay < noon)
		{
			temperature += (temperatureShift * Time.deltaTime);	
		}

is the same as

		if(GameTime._timeOfDay > GameTime.TimeOfDay.SunRise  GameTime._timeOfDay < GameTime.TimeOfDay.Noon)
		{
			temperature += (temperatureShift * Time.deltaTime);	
		}

In order to do that I had to make _timeOfDay a public static variable. However, I get an error of:

Assets/Custom Assets/Scripts/Environental/Temperature.cs(46,29): error CS0019: Operator >' cannot be applied to operands of type float’ and `GameTime.TimeOfDay’

Anyone know how to resolve this issue? If more information is necessary I’d be more than happy to provide it.

What is the datatype for GameTime.TimeOfDay?

It is a public enum, however I changed things up to where I can use the first script on the temperature script, I made Temperature.cs Derive from GameTime. There are no errors, but it does not calculate temperature when it reaches sunRise.

You are trying to compare a float with an enum, two different datatypes. But if you said you changed the things now, what’s the updated script?

To make things easier I am just going to post both scripts fully:

GameTime:

using UnityEngine;
using System.Collections;

public class GameTime : MonoBehaviour 
{
	public enum TimeOfDay
	{
		Idle,
		SunRise,
		Noon,
		SunSet,
		Midnight
	}
	
	public Transform[] sun;
	public Sun[] _sunScript;
	
	public float dayCycleInMinutes = 10;

	public float sunRise;						//the time of day that we start the sunrise
	public float noon;
	public float sunSet;						//the time of day that we start the sunset
	public float midnight;
	public float skyBoxBlendModifier;			//the speed at which the skybox blends
	
	private const float SECOND = 1;
	private const float MINUTE = 60 * SECOND;
	private const float HOUR = 60 * MINUTE;
	private const float DAY = 24 * HOUR;
	
	private const float DEGREES_PER_SECOND = 360 / DAY;
	
	private float _degreeRotation;
	
	public float _timeOfDay;
	
	private float _dayCycleInSeconds;
	
	public  TimeOfDay _tod;
	
	void Start () 
	{	
		_tod = TimeOfDay.Idle;
		_dayCycleInSeconds = dayCycleInMinutes * MINUTE;
		
		RenderSettings.skybox.SetFloat("_Blend", 0);
		
		_sunScript = new Sun[sun.Length];
		_timeOfDay = 0;
		_degreeRotation = DEGREES_PER_SECOND * DAY / _dayCycleInSeconds;
		
		for(int cnt = 0; cnt < sun.Length; cnt++)
		{
			Sun temp = sun[cnt].GetComponent<Sun>();
			
			if(temp == null)
			{
				Debug.LogWarning("Sun Script not found, adding it");
				sun[cnt].gameObject.AddComponent<Sun>();
				
				temp = sun[cnt].GetComponent<Sun>();
			}
			_sunScript[cnt] = temp;
		}
		
		sunRise *= _dayCycleInSeconds;
		noon *= _dayCycleInSeconds;
		sunSet *= _dayCycleInSeconds;
		midnight *= _dayCycleInSeconds;
	}
	
	void Update () 
	{	
		for(int cnt = 0; cnt < sun.Length; cnt++)
		{
			sun[cnt].Rotate(new Vector3(_degreeRotation, 0, 0) * Time.deltaTime);
		}
		
		_timeOfDay += Time.deltaTime;
		
		if(_timeOfDay > _dayCycleInSeconds)
		{
			_timeOfDay -= _dayCycleInSeconds;	
		}
		Debug.Log(_timeOfDay);
		
		if(_timeOfDay > sunRise  _timeOfDay < sunSet  RenderSettings.skybox.GetFloat("_Blend") < 1)
		{
			_tod = GameTime.TimeOfDay.SunRise;
			BlendSkyBox();				
		} else if(_timeOfDay > sunSet  RenderSettings.skybox.GetFloat("_Blend") > 0)
		{
			_tod = GameTime.TimeOfDay.SunSet;
			BlendSkyBox();
		} else 
		{
			_tod = GameTime.TimeOfDay.Idle;	
		}
	}
	
	private void BlendSkyBox()
	{
		float temp = 0; 
			
		switch(_tod)
		{
			case TimeOfDay.SunRise:
				temp = (_timeOfDay - sunRise) / _dayCycleInSeconds * skyBoxBlendModifier;
				break;
			case TimeOfDay.SunSet:
				temp = (_timeOfDay - sunSet) / _dayCycleInSeconds * skyBoxBlendModifier;
				temp = 1 - temp;
				break;
		}
		Debug.Log(temp);
		RenderSettings.skybox.SetFloat("_Blend", temp);
	}
}

Temperature:

using UnityEngine;
using System.Collections;

public class Temperature : GameTime 
{		
	public GUIText temper;
	public GUIText methane;
	public GUIText co2;
	public GUIText watervapor;
	public GUIText oxygen;
	
	public float temperature = -80;
	public float temperatureShift;
	public float maxTemperature = 60;
	public float minTemperature = -100;
	public float temperatureCelc;
	public float cO2 = 41.311688f;
	public float Oxygen = .56342f;
	public float Methane = 10.5f;
	public float waterVapor = .13002f;
	
	public bool isCelcius;
	public bool isFaren;
	public bool isOxygen;
	public bool isCO2;
	public bool isMethane;
	public bool isVapor;
	
	void Start () 
	{	
		temperatureShift = ((cO2 + Methane) / 9 * .15f);
		
		temper.text = ("Temperature: " + temperature + " F");
		methane.text = ("Methane: " + Methane + " nmol/mol");
		co2.text = ("Carbon Dioxide: " + cO2 + " g/mol");
		watervapor.text = ("Water Vapor: " + waterVapor + " g/mol");
		oxygen.text = ("Oxygen: " + Oxygen + " g/mol");
	}
	
	void Update () 
	{
		Calculate();
		
		Controls();
		
		if(_timeOfDay > sunRise  _timeOfDay < noon)
		{
			temperature += (temperatureShift * Time.deltaTime);	
		}
		
		if(_timeOfDay > noon  _timeOfDay < midnight)
		{
			temperature -= (temperatureShift * Time.deltaTime);	
		}
		
		if(temperature >= maxTemperature)
		{
			temperature = maxTemperature;	
		}
		
		if(temperature <= minTemperature)
		{
			temperature = minTemperature;	
		}
		
		if(isCelcius == true)
		{
			temper.text = ("Temperature: " + (temperatureCelc + 34.7777778f) + " C");				
		}
		
		if(isFaren == true)
		{
			temper.text = ("Temperature: " + temperature + " F");
		}
	}
	
	void OnGUI()
	{
		if(GUI.Button(new Rect(Screen.width - 190, Screen.height - 110, 40, 40), "F"))
		{
			isFaren = true;
			isCelcius = false;
		}
		if(GUI.Button(new Rect(Screen.width - 140, Screen.height - 110, 40, 40), "C"))
		{
			isCelcius = true;
			isFaren = false;
			Calculate();
		}
	}
	
	private void Calculate()
	{
		temperatureCelc = (temperature - 32 * 5/9);
	}
	
	public void Controls()
	{
		if(Input.GetButton("Oxygen"))
		{
			isOxygen = true;
			isCO2 = false;
			isMethane = false;
			isVapor = false;
		}
		
		if(Input.GetButton("Carbon"))
		{
			isCO2 = true;
			isOxygen = false;
			isMethane = false;
			isVapor = false;
		}
		
		if(Input.GetButton("Methane"))
		{
			isMethane = true;
			isOxygen = false;
			isCO2 = false;
			isVapor = false;
		}
		
		if(Input.GetButton("Water"))
		{
			isVapor = true;
			isOxygen = false;
			isMethane = false;
			isCO2 = false;
		}
		
		if(Input.GetButton("Increase"))
		{
			if(isOxygen == true)
			{
				Oxygen += .0001f;
			}
			
			if(isCO2 == true)
			{
				cO2 += .0001f;	
			}
			
			if(isMethane == true)
			{
				Methane += .0001f;	
			}
			
			if(isVapor == true)
			{
				waterVapor += .0001f;	
			}
		}
		
		if(Input.GetButton("Decrease"))
		{
			if(isOxygen == true)
			{
				Oxygen -= .0001f;
			}
			
			if(isCO2 == true)
			{
				cO2 -= .0001f;	
			}
			
			if(isMethane == true)
			{
				Methane -= .0001f;	
			}
			
			if(isVapor == true)
			{
				waterVapor -= .0001f;	
			}
		}
	}
}

I don’t know if you’re allowed to call Update if you already declared an Update inside the base class…Same with Start I suppose?

What I would do is that I have a function that is overridden in the child class that is called in Update. So only one instance of Update exists in both parent and child.

Plain and simple enum != float you gotta work on a way to get a float out of the enum,

public float _timeOfDay;

and the

   public enum TimeOfDay

    {

        Idle,

        SunRise,

        Noon,

        SunSet,

        Midnight

    }

http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.80).aspx <<< look at this, specifically the “int x = (int)Days.Sun;” You need to add a cast to the value otherwise it jsut reads it as a piece of the enumerator

Any references that I could check to reference a changed Update to the GameTime script? I tried Temperature.tempUpdate(); and that did not work. The start method works fine, as well as the GUI Buttons, but the Input is not working considering the Controls() function is within the Update for Temperature.cs

Did you define the input inside the input manager?

Yes this all worked previously before I put them in the Temperature script. I have changed Temperature’s Update to tempUpdate and put:

		Temperature temperature = GetComponent<Temperature>();
		temperature.tempUpdate();
		temperature.Calculate();
		temperature.Controls();

At the start of GameTime’s Update function, there are no errors, however it still does not recalculate temperature when the sunrise begins, nor does increasing or decreasing CO2/Oxygen/WaterVapor, ETC. I am going to watch the temperature in the inspector while the game is running to see if it is changing but not displaying the change.

Maybe it’s time to try putting in some debug logs to check the value of _timeOfDay and sunRise to see if that if condition is fulfilled. You might also want to check the boolean value of the variables to see if the input is working. If the input is not working, try moving one out from the function and into Update and test that single input just to see if something was stopping the function from being called (shouldn’t be the case though).

I’m sorry I couldn’t help more =/

Since changing to the tempUpdate it seems that Skyblending does not work properly, the values of CO2/Methane etc. do change when holding down the increase and decrease button but do not properly display, the temperature does not change, so i am assuming there is something with TimeOfDay

Hmmm am I not supposed to have both scripts in the game scene at the same time? I think it could possibly cause some problems since Temperature inherits from GameTime and pretty much has all of the same variables

Well, you could try removing the parent script from the gameobject in the scene to try it out. It hasn’t caused me any problems when I did that though.

Yeah it doesn’t seem to be working, it does not change the Time of Day, that is one of the main issues, the updating is also an isue. I don’t see why it’s ending up like this, I didn’t mess with time of day variables or anything

I figured out why the values of the atmospheric gases were not changing! I stupidly forgot to put anything relating to the GUIText in the Update Function, so they were never changing. The temperature on the other hand I’m not sure, and the blending not sure either. I ended up putting everything back in to that one script, too annoying at the moment.

try making the first script abstract, then just call the update from the second one and only have the second one in the scene.

Have you tried GetComponent?