Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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# 使用unity在respawn上重新启动计时器_C#_Unity3d - Fatal编程技术网

C# 使用unity在respawn上重新启动计时器

C# 使用unity在respawn上重新启动计时器,c#,unity3d,C#,Unity3d,当我从统一的悬崖上摔下来时,我试图让计时器重新启动。我已经有了一个脚本,可以让我在达到一定高度后在门槛上重生。我想做同样的事情,但不是被重生,计时器重新启动 public class Timer : MonoBehaviour { public Text timerText; private float startTime; private bool finished = false; private bool started = false;

当我从统一的悬崖上摔下来时,我试图让计时器重新启动。我已经有了一个脚本,可以让我在达到一定高度后在门槛上重生。我想做同样的事情,但不是被重生,计时器重新启动

public class Timer : MonoBehaviour {

     public Text timerText;
     private float startTime;
     private bool finished = false;
     private bool started = false;

     void Update () 
     {      
         if(!started || finished)
             return;

         float t = Time.time - startTime;

         string minutes = ((int) t/60).ToString();
         string seconds = (t%60).ToString("f2");

         timerText.text = minutes + ":" + seconds;      
     }

     public void StartTimer ()
     {
         started = true;
         startTime = Time.time;
     }

     public void StopTimer()
     {
         finished = true;
         timerText.color = Color.yellow;      
     }   
 }
我的重生脚本在我的相机装备上

public class respawn : MonoBehaviour
{
    public float threshold;

    void FixedUpdate()
    {
        if (transform.position.y < threshold)
            transform.position = new Vector3(403, 266, 337);

    }
}
公共类重生:单一行为
{
公众浮动阈值;
void FixedUpdate()
{
if(变换位置y<阈值)
transform.position=新矢量3(403266337);
}
}
你知道怎么做吗?

只需再次调用StartTimer()并在方法中重置完成:

 // ...
 public void StartTimer ()
 {
     finished = false;
     started = true;
     startTime = Time.time;
 }

 public void StopTimer()
 {
     finished = true;
     timerText.color = Color.yellow;      
 } 
不要忘记在重生脚本中停止计时器:

public Timer Timer;

void FixedUpdate()
{
    if (transform.position.y < threshold)
    {
        Timer.StopTimer();
        transform.position = new Vector3(403, 266, 337);
    }

}
公共定时器;
void FixedUpdate()
{
if(变换位置y<阈值)
{
Timer.StopTimer();
transform.position=新矢量3(403266337);
}
}

当您遇到下降条件时,只需重新启动计时器。 如果您在respawn脚本中有一个对计时器的引用,那就太好了,比如:

public class respawn : MonoBehaviour
{
    public Timer timer;
    public float threshold;

    void FixedUpdate()
    {
        if (transform.position.y < threshold)
        {
        //transform.position = new Vector3(403, 266, 337);
        timer.StartTimer();
        }

    }
}
公共类重生:单一行为
{
公共定时器;
公众浮动阈值;
void FixedUpdate()
{
if(变换位置y<阈值)
{
//transform.position=新矢量3(403266337);
timer.StartTimer();
}
}
}

你能分享你的重生代码吗?从理论上讲,它实际上应该是一样的。