Hi, this is my first time here and I’m way too new at this too.
The thing is, I made a timer and what I want to do is when it runs out of time It goes directly to the losing scene.
Here’s the script:
-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Clock : MonoBehaviour {
[Tooltip("Initial time in seconds")]
public int initialTime;
[Tooltip("Time scale of the clock")]
[Range (-10.0f,10.0f)]
public float timeScale = 1;
private Text myText;
public float frameTimeWithScale = 0f;
public float finishedTime = 0.0f;
public float secondsToShow = 0f;
public float timeScalePaused, initialTimeScale;
private bool paused = false;
public bool isPaused = false;
void Start()
{
initialTimeScale = timeScale;
myText = GetComponent<Text>();
secondsToShow = initialTime;
updateClock(initialTime);
}
void Update () {
if (!isPaused) {
frameTimeWithScale = Time.deltaTime * timeScale;
secondsToShow+= frameTimeWithScale;
updateClock(secondsToShow);
}
}
public void updateClock(float secondsToShow)
{
int minutos = 0;
int segundos = 0;
string clockText;
if (secondsToShow< 0)
secondsToShow = 0;
minutes = (int)secondsToShow/ 60;
seconds = (int)secondsToShow% 60;
clockText= minutes.ToString("0") + ":" + seconds.ToString("00");
myText.text = clockText;
}
if (isPaused) {
secondsToShow-= finishedTime;
SceneManager.LoadScene(1);
}
public void pause()
{
if (!isPaused)
{
isPaused= true;
timeScalePaused= timeScale;
timeScale= 0;
}
}
public void continue()
{
if (isPaused)
{
isPaused = false;
timeScale= timeScalePaused;
}
}
public void reset()
{
isPaused= false;
timeScale= initialTimeScale;
secondsToShow = initialTime;
updateClock(secondsToShow);
}
}
Zaeran
2
At the end of your update function, you just need:
if(secondsToShow > maxTime)
{
LoadScene(loseScene);
}
where maxTime is your variable for the amount of time before time runs out