I've looked for a simple method of doing this before but never found anything that works for me. Can anyone help me write a script for a timer that counts down (and is shown on UI as text) and when it reaches 0 it goes Scene Build +1? Please give me a quick and easy solution (preferably a script)
Consider using a Timeline sequence. There is a playables package available on the asset store that will allow you to alter UI text in Timeline: https://assetstore.unity.com/packages/essentials/default-playables-95266 Unity 2019.1 has Timeline signals which you can link to a custom function that loads the next scene index.
You could also consider using Coroutines. They make it very straightforward to implement this kind of behaviour once you get the hang of them. Example: Code (CSharp): Text timerText; void StartTimer() { StartCoroutine("Timer", 10); } IEnumerator Timer(int startTime) { int time = startTime; while(time > 0) { time = time - 1; timerText.text = time.ToString(); yield return new WaitForSeconds(1.0); } // Load next scene etc... }
I had to code a UI timer for my most recent game, here's the way I did it. There's a float I have, named TotalSeconds, and this variable decreases by Time.deltatime in the Update() function. After this, I make 2 local variables, a minutes, and a seconds. Since TotalSeconds is in seconds, we can set Minutes to TotalSeconds/60, and we can set the seconds to TotalSeconds % 60. That returns the remainder after dividing the total seconds by 60. Doing these 2 assignments will convert any amount of seconds you put in into a minute variable and a second variable. You can simply combine them in a string then! Code (CSharp): //I used Mathf.FloorToInt to ensure I don't get any weird numbers min = Mathf.FloorToInt (totalSeconds / 60); sec = Mathf.FloorToInt (totalSeconds % 60);