简体   繁体   English

Unity 简单的倒数计时器不起作用(C#)

[英]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.我正在尝试在 Unity 中为我的游戏添加一个简单的倒数计时器,但计时器在游戏中不会 go 下来。 Programming language is C#.编程语言为 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.我认为 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.我觉得我没有犯任何错误,我的 Unity 控制台中也没有收到任何编译错误。 Thank you.谢谢你。

You need to put your started variable to true in the Start() with started = true;您需要在Start()中使用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.如果你不这样做, Update()中的if (started)总是 false,所以里面的代码永远不会运行。

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.因为您从不从 start 方法调用 StartTimer。 your started variable is always false and never gets updated.started的变量总是假的,永远不会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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