Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 碰撞后触发计时器_C#_Unity3d_3d - Fatal编程技术网

C# 碰撞后触发计时器

C# 碰撞后触发计时器,c#,unity3d,3d,C#,Unity3d,3d,我正在尝试制作一个游戏,在与其中一个角色发生碰撞后计时器被触发。我努力为之制定正确的代码。以下是我所拥有的: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Timer : MonoBehaviour { public GameObject textDisplay; public int secondsLef

我正在尝试制作一个游戏,在与其中一个角色发生碰撞后计时器被触发。我努力为之制定正确的代码。以下是我所拥有的:

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

public class Timer : MonoBehaviour
{
  public GameObject textDisplay;
  public int secondsLeft = 30;
  public bool takingAway = false;

void OnTriggerEnter(Collider other) instead of Start?
{
 if (other.CompareTag("Player"))
 {
textDisplay.GetComponent<Text>().text = "00:" + secondsLeft = true;
 }

void Start(){
  textDisplay.GetComponent<Text>().text = "00:" + secondsLeft;
}

void Update()
{
  if (takingAway == false && secondsLeft > 0)
  {
    StartCoroutine(TimerTake());
  }
}

IEnumerator TimerTake()
{
  takingAway = true;
  yield return new WaitForSeconds(1);
  secondsLeft -= 1;
  textDisplay.GetComponent<Text>().text = "00:" + secondsLeft;
  takingAway = false;
}

}

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类计时器:单行为
{
公共游戏对象文本显示;
公共int secondsLeft=30;
公共布尔塔金加韦=假;
无效的OnTriggerEnter(碰撞器其他)而不是Start?
{
如果(其他比较标记(“玩家”))
{
textDisplay.GetComponent().text=“00:+secondsLeft=true;
}
void Start(){
textDisplay.GetComponent().text=“00:+secondsLeft;
}
无效更新()
{
如果(takingAway==false&&secondsLeft>0)
{
start例程(TimerTake());
}
}
IEnumerator TimerTake()
{
takingAway=真;
返回新的WaitForSeconds(1);
第二个sleft-=1;
textDisplay.GetComponent().text=“00:+secondsLeft;
takingAway=假;
}
}

听起来您正在尝试的是:

  • 每秒钟减少一秒钟
  • 如果播放器发生碰撞,请停止计时器
我可能会这样做

public class Timer : MonoBehaviour
{
    // directly give your field the correct type
    public Text textDisplay;
    public int secondsLeft = 30;

    // Optionally add some events to be invoked 
    // these are like the onClick of Buttons

    // This event is invoked when the player didn't reach in time
    public UnityEvent whenTimedOut;
    // This event is invoked when the player stopped the timer
    public UnityEvent whenStoppedByPlayer;

    // If return type is IEnumerator Unity runs Start 
    // automatically as Coroutine
    private IEnumerator Start()
    {   
        while(secondsLeft > 0)
        {
            textDisplay.text = "00:" + secondsLeft;
            yield return new WaitForSeconds(1);
            secondsLeft --;     
        }

        textDisplay.text = "00:" + secondsLeft;
        whenTimedOut.Invoke();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            // by disabling this component also the Coroutine is stoppepd
            enabled = false;
            textDisplay.text = "00:" + secondsLeft;
  
            whenStoppedByPlayer.Invoke();
        }
    }
}

或者你是想在玩家触发计时器后启动它吗?在这种情况下,你也可以直接将其作为一个协同程序运行,例如

private IEnumerator OnTriggerEnter(Collider other)
{
    if (!other.CompareTag("Player")) yield break;
    
    while(secondsLeft > 0)
    {
        textDisplay.text = "00:" + secondsLeft;
        yield return new WaitForSeconds (1);
        secondsLeft--;
    }
    
    textDisplay.text = "00:" + secondsLeft;

    whenTimedOut.Invoke();
}

是的,我试图在玩家触发计时器后启动它。非常感谢!这是我使用的最后一个代码:
使用System.Collections;使用System.Collections.Generic;使用UnityEngine;使用UnityEngine.UI;公共类倒计时:MonoBehavior{public Text textDisplay;public int secondsLeft=30;private IEnumerator OnTriggerEnter(碰撞器其他){if(!other.CompareTag(“Player”))产生中断;而(secondsLeft>0){textDisplay.Text=“00:”+secondsLeft;产生返回新的WaitForSeconds(1);secondsLeft--;}textDisplay.text=“00:+secondsLeft;}}