简体   繁体   中英

Script for setting CountDown timer and Canvas does not play properly in Unity

I'm building a first person VR shooting game and the script responsible for CountDown and displaying Canvas on which Play Again Button appears does not play..

Here is the screen of the game play and the script for better understanding:

游戏

`

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

public class playGameAgainScript : MonoBehaviour {

    //Use this for Timer
    public Text countDown;
    private float timer = 60;

    //Declare Button
    public Button playAgainButton;

    void Start() {
        //Set countDown value
        countDown = GetComponent<Text>();
    }

    void Update() {
        timer -= Time.deltaTime;
        countDown.text = "" + timer.ToString ("f0");
        if (timer <= 0) {
            timer = 0;
            playAgainButton.gameObject.SetActive (true);
        } 
    }

    public void PlayAgain() {
        UnityEngine.SceneManagement.SceneManager.LoadScene ("Main Scene");
    }
}

`

I edited the code you provided a little bit and hope that it works

 private bool counting=true,
 void Update() {
if(counting){
        timer -= Time.deltaTime;
        countDown.text = "" + timer.ToString ("f0");
        if (timer <= 0f) {
            counting=false;
            playAgainButton.gameObject.SetActive(true);
        }
}
}

notice that in your code the condition of timer<=0 is always valid when you make timer=0; now it will enter just one time to the condition and it should work.

问题是playGameAgainButton默认设置为非活动状态,因此编辑器不需要查找。通过将功能从此脚本移动到其他位置,它解决了问题。.我将其移至主playerScript和它工作正常。

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