简体   繁体   中英

Unity simple countdown timer not working (C#)

I'm trying to add a simple countdown timer to my game in Unity but the timer does not go down in game. Programming language is C#.

Here's my code:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;

public class GameManager : MonoBehaviour
{
    public static GameManager instance;

    private int score, highScore;
    public Text scoreText, highScoreText;

    private float time;
    public Text timeText;

    public bool started, gameOver;

    public GameObject gameOverPanel;
    public Text gameOverScore, gameOverHighScore;
    public Button playAgain;

    void Start()
    {
        instance = GetComponent<GameManager>();

        score = 0;
        scoreText.text = "Score: " + score;
        highScore = PlayerPrefs.GetInt("HighScore");
        highScoreText.text = "Highscore: " + highScore;

        time = 45;
        gameOver = false;
        UpdateTime();
    }

    void Update()
    {
        if (started)
        {
            time -= Time.deltaTime;
            UpdateTime();
            if (time <= 0)
            {
                GameOver();
            }
        }
    }

    public void IncreaseScore()
    {
        score++;
        scoreText.text = "Score: " + score;
        time += 5;
        UpdateTime();
    }

    public void StartTimer()
    {
        time = 45;
        started = true;
    }

    public void UpdateTime()
    {
        string minutes = Mathf.Floor(time / 60).ToString("00");
        string seconds = Mathf.Floor(time % 60).ToString("00");
        timeText.text = string.Format("Time: {0}:{1}", minutes, seconds);
    }

    private void GameOver()
    {
        time = 0;
        UpdateTime();
        started = false;
        gameOver = true;
        gameOverPanel.SetActive(true);
        gameOverScore.text = "Final Score: " + score;
        if (score > highScore)
        {
            gameOverHighScore.text = "High Score: " + score;
            PlayerPrefs.SetInt("HighScore", score);
        }
        else
        {
            gameOverHighScore.text = "High Score: " + score;
        }
    }

    public void LoadGame()
    {
        SceneManager.LoadScene("GameScene");
    }
}

I think there's an issue somewhere in the UpdateTime() function. I mostly followed tutorials online to get here as I'm only a beginner so please don't judge too harshly and I'll try my best to understand any answers you give me. I feel I haven't made any mistakes and I'm not receiving any compilation errors in my Unity console either. Thank you.

You need to put your started variable to true in the Start() with started = true;

void Start() {
    instance = GetComponent<GameManager>();

    score = 0;
    scoreText.text = "Score: " + score;
    highScore = PlayerPrefs.GetInt("HighScore");
    highScoreText.text = "Highscore: " + highScore;

    time = 45;
    gameOver = false;
    UpdateTime();
    started = true; //HERE
}

If you dont do that, the if (started) in the Update() is always false, so the code inside will never run.

you never call start timer.

your start method should look like this

void Start()
{
    instance = GetComponent<GameManager>();

    score = 0;
    scoreText.text = "Score: " + score;
    highScore = PlayerPrefs.GetInt("HighScore");
    highScoreText.text = "Highscore: " + highScore;

    gameOver = false;
    StartTimer();
    UpdateTimer();
}

Because you never call StartTimer from your start method. your started variable is always false and never gets updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM