Why is refrence to parent variable not updating

So I’ve looked up the problem and asked someone else option but to no result. I have a variable trying to reference the parents script variable and use it to update. The parent variable is updating but the variable referenced to it is not updating after its first referenced.

default parent variable:

public float time { get; private set;} //2 to 26 hours

code to refrence new variable to parent:

void Awake () {
		renderColor = this.GetComponent<SpriteRenderer> ().color;
		time = this.transform.GetComponentInParent<DayNightTime> ().time;
	}

	void FixedUpdate (){

		Debug.Log (time);
		if(time >= 5 && time < 8){
			float percentage = ((time - 5) * 100) / 8 - 5;
			renderColor = Color.Lerp(nightColor, dayColor, percentage);
		}else if (time >= 19 && time < 21) {
			float percentage = ((time - 19) * 100) / 19 - 21;
			renderColor = Color.Lerp(dayColor, nightColor, percentage);
		}
	}

I’ve been a bit frustrated using different methods to see if the problem would be fixed. I’m hoping I’m not doing anything stupid but I would be grateful for someone to possibly point me in the right direction!

Good day.

First, as a good practice yo umust know if the “if sentences” are working fine, if all variables get assigned, etc.

Debug your code while executing, and/or make some Debug.Log with all the variables after they are suposed to be assigned… this way you can detect possible fails, or if some if/else if is not working as you desire.

Another way to be sure you are getting the correct compoenent of a parent is this

time = gameObject.transform.parent.gameObject.GetComponent().time;
renderColor = gameObject.GetComponent<SpriteRenderer> ().color;

( think that “this” refears to this script, the component, not the gameobject containing it… thats why is better use always “gameObject”, in some cases it will prevent to refear the wrong thing. In this case is exactly the same, but you know, is a good practice)

Explore the results of Debugs.Log and say what you get, maybe you find the solution by your own. If not, come and say what results you get. As more information we have, best answer you will get

Bye!!
:smiley:


Extension:

You must know how the coputer executes the codes.

Take a look at Unity Execution Order to know what you need.

Is possible you need that scriptA send some information to scriptB so scriptB can send other information to scriptA. All of this before the first Update. So you must know how are they executed.

Each frame, will commence for ALL Awakes of new objects, then ALL OnEnables, then ALL Starts, then ALL Updates. Will not begin this cycle until all Updates has finished.

So think where you need to write every code, to get the correct information flow.

Tip: You can always make a bool variable like “Initializaed” and use the first update to still configure things… :smiley:

Bye

Goiod luck!
Bye!

Thanks for the tip on using gameobject instead. So I switched over to gameobject and I have been testing the Debug.Log() outside of the if statement but it seems that the:


  • refrenced variable grabs the value before the start method on the parent is called.

I found this interesting as the
start method on the parent gives the value to its variable from 2 to 26 which works fine. So if the variable is being accessed before going through the start statement does that mean that object is not created yet and the child is created first? Seems weird.



EDIT: So now I got that minor problem fixed: I switched awake and Start and I at least get the varaible created in that, still the refrence doesnt seem to update after assigned the first number though so more testing is needed to find out why