Does GUI update every time a scene is loaded?
I am attempting to make my first game in Unity so I am very new to the process. Its a basic game based on the time it takes you to complete a level. I have a scene that loads after the player has completed the level which tells you your time and if you have beat the best time or not. I noticed after playing through one time then attempting to play through again, the GUI for "your time" only displays the last time you played through and not the current. Not sure if its my code or logic error, but its an important part of the game and can't figure it out! This is the code attached to an object in my "level one win game scene"....
using UnityEngine; using System.Collections; public class Lvl1_WinGame : MonoBehaviour { %|-268258433_1|% %|618304092_2|% private int seconds = (playTime % 60); %|-1959320627_4|% %|1587356682_5|% %|952064105_6|% %|-1404413236_7|% %|-816941512_9|% %|1611647783_10|% GUI.color = Color.black; %|-1444865868_12|% %|-1195048236_13|% %|593022224_14|% %|-2127596018_15|% %|-1085200602_16|% %|875474438_17|% %|-1404530180_18|% %|-1783748057_19|% else if (minutes == minutes2 && seconds == seconds2) %|-1739097657_21|% %|-466144641_22|% %|1662746052_23|% %|922373008_25|% %|-82144881_26|% GUI.Label (new Rect ((Screen.width/2)-195,(Screen.height/2)+80, 400, 50), "You didnt beat the best time..."); %|-1911277770_28|% %|-1809811350_30|% %|-2122394016_31|% %|503576203_32|% }
This is my first question on unity forums so i'm not sure what other information to provide, let me know if there is anything else that I can provide. Any help would be appreciated!
using UnityEngine;
using System.Collections;
public class Lvl1_WinGame : $$anonymous$$onoBehaviour
{
public static int playTime = Timer.playTime;
private int $$anonymous$$utes = (playTime / 60) % 60;
private int seconds = (playTime % 60);
private int $$anonymous$$utes2;
private int seconds2;
private int textSize = 20;
public Font font;
void OnGUI()
{
GUI.color = Color.black;
GUI.Label (new Rect ((Screen.width/2)-195,Screen.height/2, 400, 50), "Your Time = " + $$anonymous$$utes.ToString() + " $$anonymous$$inutes " + seconds.ToString() + " Seconds ");
GUI.Label (new Rect ((Screen.width/2)-195,(Screen.height/2)+40, 400, 50), "Best Time = " + $$anonymous$$utes2 + " $$anonymous$$inutes " + seconds2 + " Seconds ");
if (($$anonymous$$utes < $$anonymous$$utes2 && seconds < seconds2) || ($$anonymous$$utes == $$anonymous$$utes2 && seconds < seconds2) || ($$anonymous$$utes < $$anonymous$$utes2 && seconds > seconds2))
{
GUI.Label (new Rect ((Screen.width/2)-195,(Screen.height/2)+80, 400, 50), "You beat the best time!");
}
else if ($$anonymous$$utes == $$anonymous$$utes2 && seconds == seconds2)
{
GUI.Label (new Rect ((Screen.width/2)-195,(Screen.height/2)+80, 400, 50), "You tied the best time!");
}
else if (($$anonymous$$utes > $$anonymous$$utes2 && seconds > seconds2) || ($$anonymous$$utes==$$anonymous$$utes2 && seconds>seconds2))
{
GUI.Label (new Rect ((Screen.width/2)-195,(Screen.height/2)+80, 400, 50), "You didnt beat the best time...");
}
GUI.skin.label.fontSize = textSize;
GUI.skin.label.font = font;
}
}
Answer by Nazirzadeh · May 06, 2016 at 04:22 PM
It does not change because your playTime is static!
playTime should not be static.
Ohhh okay thank you, yeah that makes since. But now I'm not sure how to do the initialization of "$$anonymous$$utes" and "seconds", because its saying a field initializer cannot reference a non static field. Should I initialize them in Start()?
Your answer
![](https://koobas.hobune.stream/wayback/20220612080305im_/https://answers.unity.com/themes/thub/images/avi.jpg)