Need help with a timer in a android 2d game
Hello, I'm new to unity, I'm working on my first project for 2 weeks and I can't figure this out: I have created a timer script to my game scene and if the player loses, I have a new scene, a lose scene with a button that I wish to make it load the game scene with +5 seconds to the timer OnClick. How do I increment my timer ? My code:
This is the timer:
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Diagnostics;
public class countTimer : MonoBehaviour { public float currentTime = 0f; public float startingTime = 10f;
[SerializeField] Text countdownText;
void Start()
{
currentTime = startingTime;
}
void Update()
{
currentTime -= 1 * Time.deltaTime;
countdownText.text = currentTime.ToString("0");
if (currentTime <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
}
}
}
And this is my lose game scene:
using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; using UnityEngine.SceneManagement;
public class LoseWindow : MonoBehaviour { public void back_menu() {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 2);
}
}
How should I tackle this problem??? Please help a new guy. Thanks
Your answer
Follow this Question
Related Questions
Making A Countdown Timer 0 Answers
How to make timer wait before resetting? 1 Answer
Countdown Timer 1 Answer
Is there a way to calculate elapsed Time in total? (Even when the App is closed etc.) 1 Answer
Display cooldown timer 1 Answer