C# 使用TIMESCENCELEVELLOAD变量的If循环

C# 使用TIMESCENCELEVELLOAD变量的If循环,c#,unity3d,C#,Unity3d,我有一个切换按钮,它被初始化为true,当它被关闭时,它应该触发一个函数,对经过的每一秒进行减法 要减去的变量名为温度,它应该每秒减去1,直到达到0。为了计算秒数,我使用名为TimeSinceLevelLoad的MonoBehavior变量 我建立了I函数,但是减法永远不会发生,你能帮我吗 using UnityEngine; using System.Collections; using Assets.Code.PowerPlants; public class thermoPowerCon

我有一个切换按钮,它被初始化为true,当它被关闭时,它应该触发一个函数,对经过的每一秒进行减法

要减去的变量名为温度,它应该每秒减去1,直到达到0。为了计算秒数,我使用名为TimeSinceLevelLoad的MonoBehavior变量

我建立了I函数,但是减法永远不会发生,你能帮我吗

using UnityEngine;
using System.Collections;
using Assets.Code.PowerPlants;

public class thermoPowerControlPanel : MonoBehaviour {

    private ThermoElectric thermo;

    public bool t1= true;
    int temperature;
    private int tempUP = 10;
    private int tempDOWN = 1;
    private int mark;
    private int i =0;

    public thermoPowerControlPanel (){
        temperature = 100;
    }

    public void turbine1State (bool t1) {
        Debug.Log (temperature);

        if (i ==0) {
            mark = (int)Time.timeSinceLevelLoad;
            i = 1;
        } 

        if (t1 == false) {
        ThermoElectric.t1Bool = t1;
            if (temperature != 0) { 
                    if (mark + 1  == (int)Time.timeSinceLevelLoad ) {
                        temperature = temperature - tempDOWN;
                        i = 0;
                    Debug.Log (temperature);
                    }
            }
        }
    }
    }

boolt1工作正常,我已经检查过了,它变为False。

这有点太复杂了。也许你应该调查一下合作项目

您可以创建这样的方法(协同程序

private IEnumerator decreseTemperature()
{
    for (int i = 0; i < temperature; i++) // this will make couroutine cycle
    {
        yield return new WaitForSeconds(1.0f); // this makes coroutine wait for 1 second
        temperature = temperature - tempDown;
    }
}

您可能需要根据自己的需要调整此代码,但协同程序在此类问题中非常有用。

这有点太复杂了。也许你应该调查一下合作项目

您可以创建这样的方法(协同程序

private IEnumerator decreseTemperature()
{
    for (int i = 0; i < temperature; i++) // this will make couroutine cycle
    {
        yield return new WaitForSeconds(1.0f); // this makes coroutine wait for 1 second
        temperature = temperature - tempDown;
    }
}

您可能需要根据自己的需要调整此代码,但协同路由在此类问题中非常有用。

非常感谢您的回答,也许我可以使用此条件来代替尝试,我真的发现协同路由非常有用。祝你好运对于(int i=0;temperature>0;i++),我需要处理这个问题,但我会给出答案,因为这是一个很好的解决方案。将ienumerable更改为ienumerator。现在这是正确的,还请记住,您必须在循环条件中使用“i”。非常感谢您的回答,也许我可以使用这个条件,而不是尝试它,我真的发现协程很有用。祝你好运对于(int i=0;temperature>0;i++),我需要处理这个问题,但我会给出答案,因为这是一个很好的解决方案。将ienumerable更改为ienumerator。现在这是正确的,还请记住,在循环条件中必须使用“i”