【问题标题】:Script for setting CountDown timer and Canvas does not play properly in UnityUnity 中设置倒计时和 Canvas 的脚本无法正常播放
【发布时间】:2017-02-16 13:17:39
【问题描述】:

我正在构建一个第一人称 VR 射击游戏,但负责倒计时和显示出现“再次播放”按钮的 Canvas 的脚本无法播放..

这里是游戏画面和脚本以便更好地理解:

`

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");
    }
}

`

【问题讨论】:

  • 为什么你有一个按钮组件,还有一个EventTrigger?你的画布有 GraphicsRaycaster 吗?
  • 因为我需要一个 PointerEnter 功能.. 是的,画布有一个 GraphicsRaycaster..

标签: c# android unity3d virtual-reality google-vr


【解决方案1】:

我稍微编辑了您提供的代码,希望它能正常工作

 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);
        }
}
}

请注意,在您的代码中,当您设置 timer=0 时,timer

【讨论】:

  • 感谢您的提示,但这不是问题所在.. 我明白您在那里所做的,但代码似乎工作正常.. 我觉得它只是没有被实例化.. Unity 没有抛出错误,但倒计时(红色的 60)没有开始..
  • 好的,尝试删除 countDown = GetComponent();从 Start 函数中手动分配。
  • 问题是 playGameAgainButton 默认设置为非活动状态,因此编辑器没有可查找的内容。通过将功能从该脚本移到其他位置,它解决了问题。我把它移到了主 playerScript 上,它工作正常。还是谢谢!
【解决方案2】:

问题在于 playGameAgainButton 默认设置为非活动状态,因此编辑器没有可查找的内容。 并且通过将功能从这个脚本移动到不同的地方,它解决了这个问题。我把它移动到主 playerScript 并且它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    相关资源
    最近更新 更多