nothing happens when changing variable values, only in inspector

Hey all,
I have a script to change the Position of the Sun based on the real Time. Now i have Buttons to change the hour and minutes.
When i start the Scene in GameMode, i can change the value of hour and minutes in the inspector manually and the Sun is changing his position. But using the Buttons only changes the values in the inspector but my Sun is not chaning his position :confused:
I have a Light GameObject with the Sun Script :
with hour and minutes

[SerializeField]
[Range(0, 24)]
public int hour;
[SerializeField]
[Range(0, 60)]
public int minutes;

full Script that i use is here https://gist.githubusercontent.com/paulhayes/54a7aa2ee3cccad4d37bb65977eb19e2/raw/96574ed9041f15902672b92ac7ef80268bbc27b8/Sun.cs
with an other Script in a different GameObject i change the values like this:

if(name == “hour_up”)
{
mysunpos.GetComponent<Entropedia.sunpos>().hour += 1;
}

if the ray hits the button hour_up it should increase the hour by one. Well it changes the value in the inspector but the sun position doesnt change :confused:
i tried to disable the sun script to change the value, but when i enable it again it still doesnt change the sun position. Only when i use my mouse to enable it again in the inspector, then the sun is chaning his position.
someone got an idea?

What is the result of this comparison? Use Debug.Log() to print name immediately before that check.

Also, is the code you posted even running? Put another Debug.Log() in there and see if it prints out!

Your time variable which seems to actually drive the position of your sun only gets updated in OnValidate(). Since you’re setting the hour field directly, OnValidate() is not being called. Perhaps try changing the time with your SetTime() method instead of setting the hour directly. That way OnValidate will be called.

This is part of why Unity’s “default” of using public fields everywhere is bad. You often end up exposing a bit too much about the internals of your classes than you really should.

1 Like

Thank you! I got it working like you explained.