Hi! i’m new with c# and unity, I need help with a clicking counting game timer countdown, i tryed alot of codes but still not working and so far i found a code, it shows just sec. and it works fine but it wont stop at 0 and it keeps counting in subtract after 0. also i want to add codes that the timer start when i start clicking counting and stops counting clicks when it reaches 0
please help!
thank you
using UnityEngine;
using System.Collections;
public class Click : MonoBehaviour {
float timeLeft = 60.0f;
public UnityEngine.UI.Text Time;
public UnityEngine.UI.Text goldDisplay;
public int gold = 0;
public int goldperclick = 1;
void Update()
{
goldDisplay.text = "Score: " + gold;
timeLeft -= UnityEngine.Time.deltaTime;
Time.text = "" + Mathf.Round(timeLeft);
if (timeLeft < 0)
{
}
}
public void Clicked()
{
gold += goldperclick;
}
}
Add a bool for when the game ends to stop the timeLeft from going down and pin the value.
using UnityEngine;
using System.Collections;
public class Click : MonoBehaviour
{
float timeLeft = 60.0f;
public UnityEngine.UI.Text Time;
private bool isGameStarted = false;
public UnityEngine.UI.Text goldDisplay;
public int gold = 0;
public int goldperclick = 1;
private bool gameEnded = false;
void Update()
{
goldDisplay.text = "Score: " + gold;
if (timeLeft > 0 && gameEnded == false)
{
timeLeft -= UnityEngine.Time.deltaTime;
Time.text = "" + Mathf.Round(timeLeft);
if (timeLeft < 0)
{
timeLeft = 0;
isGameStarted = false;
gameEnded = true; // prevent from calling all the text updates after time is less than 0 and pin it at 0
}
}
if (gameEnded == true)
{
// do something, popup a game ended canvas, load a level, whatever.
}
}
public void Clicked()
{
if (isGameStarted)
{
gold += goldperclick;
}
else
{
isGameStarted = true; // isGameStarted is false on the first click
// if you want to get gold on the first click uncomment out below
gold += goldperclick;
}
}
}