Unity C# 计时器应每 1.5 分钟触发一次功能,但不会
Unity C# timer should trigger function every 1.5 min but doesn't
我正在尝试编写一个脚本,在 1.5 分钟后将所有灯关闭 10 秒,然后再将它们重新打开。
现在,似乎计时器被绕过了。我意识到这样做的原因可能是因为时间永远不会正好是 90 左右。
话虽如此,我不知道如何获得我想要的结果。
我想用 InvokeRepeating
代替(如注释掉的那样),但这意味着灯每次都会关闭更长时间。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsTimerTrigger : MonoBehaviour {
private GameObject[] allLights;
private float time = 0.0f;
private float secondTimer = 0.0f;
void Start () {
// Create array of lights
allLights = GameObject.FindGameObjectsWithTag("riddleLights");
//InvokeRepeating("lightsOn", 60.0f, 120.0f);
//InvokeRepeating("lightsOff", 60.10f, 120.10f); // Exponential
}
// Update is called once per frame
void Update () {
time += Time.deltaTime;
if(time%90 == 0)
{
secondTimer = time;
lightsOff();
Debug.Log("Lights off");
}
if (time == secondTimer + 10)
{
// Turn lights back on after 10 seconds
lightsOn();
Debug.Log("Lights back on");
}
}
void lightsOff()
{
foreach (GameObject i in allLights)
{
i.SetActive(false);
}
}
void lightsOn()
{
foreach (GameObject i in allLights)
{
i.SetActive(true);
}
}
}
if(time%90 == 0)
这(几乎可以肯定)永远不会是真的。
如果时间是 90.000000001 会怎样?好吧,除掉 90 部分并检查 if(0.000000001 == 0)
这是错误的。
您应该这样更正您的代码:
if(time >= 90) {
time -= 90;
//the rest of your code
}
您必须为 10 秒的延迟执行类似的操作。
协程选项:
void Start () {
//existing code
StartCoroutine(FlickerLights());
}
IEnumerator FlickerLights() {
while(true) {
yield return new WaitForSeconds(90);
lightsOff();
Debug.Log("Lights off");
yield return new WaitForSeconds(10);
lightsOn();
Debug.Log("Lights back on");
}
}
我正在尝试编写一个脚本,在 1.5 分钟后将所有灯关闭 10 秒,然后再将它们重新打开。
现在,似乎计时器被绕过了。我意识到这样做的原因可能是因为时间永远不会正好是 90 左右。
话虽如此,我不知道如何获得我想要的结果。
我想用 InvokeRepeating
代替(如注释掉的那样),但这意味着灯每次都会关闭更长时间。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsTimerTrigger : MonoBehaviour {
private GameObject[] allLights;
private float time = 0.0f;
private float secondTimer = 0.0f;
void Start () {
// Create array of lights
allLights = GameObject.FindGameObjectsWithTag("riddleLights");
//InvokeRepeating("lightsOn", 60.0f, 120.0f);
//InvokeRepeating("lightsOff", 60.10f, 120.10f); // Exponential
}
// Update is called once per frame
void Update () {
time += Time.deltaTime;
if(time%90 == 0)
{
secondTimer = time;
lightsOff();
Debug.Log("Lights off");
}
if (time == secondTimer + 10)
{
// Turn lights back on after 10 seconds
lightsOn();
Debug.Log("Lights back on");
}
}
void lightsOff()
{
foreach (GameObject i in allLights)
{
i.SetActive(false);
}
}
void lightsOn()
{
foreach (GameObject i in allLights)
{
i.SetActive(true);
}
}
}
if(time%90 == 0)
这(几乎可以肯定)永远不会是真的。
如果时间是 90.000000001 会怎样?好吧,除掉 90 部分并检查 if(0.000000001 == 0)
这是错误的。
您应该这样更正您的代码:
if(time >= 90) {
time -= 90;
//the rest of your code
}
您必须为 10 秒的延迟执行类似的操作。
协程选项:
void Start () {
//existing code
StartCoroutine(FlickerLights());
}
IEnumerator FlickerLights() {
while(true) {
yield return new WaitForSeconds(90);
lightsOff();
Debug.Log("Lights off");
yield return new WaitForSeconds(10);
lightsOn();
Debug.Log("Lights back on");
}
}