- Home /
Time does not start counting down when need to
Hello, im making 2d mobile game and i came to problem. So my game starts on click and timers and every element shows on that click and needs to start when i touch the screen, and here is the problem, my timer of game doesnt start countdown on click it just shows himself, i know this code isnt right i tried to fix it with another method and all kinds of stuff, but no luck. Btw my timer is in update function. Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; using UnityEngine.SceneManagement;
public class TimerIIgra : MonoBehaviour {
//za tajmer 30s
private float timerIgre = 30f;
[SerializeField] private Text textTimeraIgre;
[SerializeField] private Text textFinishedScore;
[SerializeField] private Text tapCounterText;
public int tapCounter = 0;
[SerializeField] public GameObject retryButton;
[SerializeField] public GameObject mainMenuButton;
//public countDownTimer countDownTimerScript;
public bool gameStart = false;
//public bool gameStarted = true;
public Animator animator;
void Start () {
//textTimeraIgre = GetComponent<Text>();
textTimeraIgre.text = "";
tapCounterText.text = "";
textFinishedScore.text = "";
retryButton.SetActive(false);
mainMenuButton.SetActive(false);
animator = GetComponent<Animator>();
//if (Input.GetMouseButtonDown(0))
//{
//gameStart = true;
//}
//StartGame();
}
public void Retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void BackToMenu()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
/*public void StartGame()
{
if (Input.GetMouseButtonDown(0))
{
gameStart = false;
}
}*/
void Update()
{
//if (Input.GetMouseButtonDown(0))
//{
//gameStart = true;
if (gameStart == true)
{
textTimeraIgre.text = "30";
timerIgre -= Time.deltaTime;
textTimeraIgre.text = timerIgre.ToString("f0");
if (Input.GetMouseButtonDown(0))
{
animator.SetBool("isPressed", true);
tapCounter++;
tapCounterText.text = tapCounter.ToString();
}
else
{
animator.SetBool("isPressed", false);
}
if (timerIgre <= 0)
{
tapCounterText.text = "";
gameStart = false;
textTimeraIgre.text = timerIgre.ToString("Time's Up!");
textFinishedScore.text = "Score: " + tapCounter.ToString();
//FindObjectOfType<GameManager>().EndGame();
retryButton.SetActive(true);
mainMenuButton.SetActive(true);
}
}
//}
}
}
Answer by logicandchaos · Apr 19, 2020 at 04:39 PM
You should move everything in Update in StartGame, then use this for update
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartGame();
}
}
to start with, let me know how things go.
I did, it starts on click but i have another problem now, my timer (30sec) does not start counting down, do you maybe know?
Your answer
Follow this Question
Related Questions
CountDown Timer Help (Seconds problem) 2 Answers
Timer reset 1 Answer
countdown timer acivation 1 Answer