等待变量变化的c#循环不起作用

本文关键字:循环 不起作用 变量 变化 等待 | 更新日期: 2023-09-27 18:06:54

我有两个循环,一个循环是永远的,另一个在循环中,它应该等待变量"month"的变化。下面是代码

int monthc = 0;
while (true) {
            monthc = time.month + 1;
            while(monthc != time.month){
            Debug.Log (monthc + " " + time.month);
            yield return new WaitForSeconds (0.1f);
        }
        monthc = 0;
            payment = loan * interest;
            money.Money = money.Money - payment;
        }

如果你想知道yield return new WaitForSeconds (0.1f);它有点像thread.sleep(100);;

不确定是否需要时间类,所以我将包括它无论如何;

public static int daytime = 1;
public static int month = 1;
public static int year = 1;
public static float speed = 2f;
Text text;
IEnumerator Time(){
    while (true) {
        daytime++;
        yield return new WaitForSeconds (speed);
        if (daytime == 31) {
            daytime = 0;
            month++;
        }
        if (month == 13) {
            year++;
            month = 0;
        }
    }
}

等待变量变化的c#循环不起作用

你的问题至少有一个在这里:

monthc = time.year + 1;
    while(monthc != time.year + 1){
    ....

您将monthc更改为等于time.year + 1,因此它们总是相等的,因此永远不会进入内部while循环