C# 为什么我的变量被其他函数引用?

C# 为什么我的变量被其他函数引用?,c#,unity3d,C#,Unity3d,我发生了一件很奇怪的事。我有一个脚本,可以在4种不同的波之间进行选择。这些波有自己的特性。但是,有时一个波的特性将用于另一个波 代码如下。如你所见,有四种不同类型的波。调试将状态为Wave1或Wave2。它还将更新编辑器中的所有变量,但是实际看到的可能是来自Wave 3的实例化和来自Wave 4的变量,但调试将显示Wave 1 你知道为什么会这样吗 void Start() { waveActive = false; dropheight = 100; } // Updat

我发生了一件很奇怪的事。我有一个脚本,可以在4种不同的波之间进行选择。这些波有自己的特性。但是,有时一个波的特性将用于另一个波

代码如下。如你所见,有四种不同类型的波。调试将状态为Wave1或Wave2。它还将更新编辑器中的所有变量,但是实际看到的可能是来自Wave 3的实例化和来自Wave 4的变量,但调试将显示Wave 1

你知道为什么会这样吗

 void Start() {
    waveActive = false;
    dropheight = 100;

}

// Update is called once per frame
void Update()
{
    if(waveActive == false)
    {
        StartCoroutine(SelectWave());
    }
}

IEnumerator SelectWave()
{
    waveActive = true;
    float val = Random.value;
    if(val <= .25)
    {
        InvokeRepeating("Wave1", delay, delay);
    }
    if (val > .25 && val <= .50)
    {
       InvokeRepeating("Wave2", delay, delay);
    }
    if (val > .5 && val <= .75)
    {
        InvokeRepeating("Wave3", delay, delay);
    }
    if (val > .75)
    {
        InvokeRepeating("Wave4", delay, delay);
    }
    yield return new WaitForSeconds(Random.Range(20f,30f));
    CancelInvoke();
    waveActive = false; 
}

void Wave1()
{
    Debug.Log("Wave1");
    delay = .5f;
    Instantiate(smallFlame, new Vector3(drop1, dropHeight, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop2, dropHeight, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop3, dropHeight, 0), Quaternion.identity);

}
void Wave2()
{
    Debug.Log("Wave2");
    delay = 1f;

    Instantiate(largeFlame, new Vector3(drop1, dropHeight, 0), Quaternion.identity);
    Instantiate(largeFlame, new Vector3(drop2, dropHeight, 0), Quaternion.identity);
    Instantiate(largeFlame, new Vector3(drop3, dropHeight, 0), Quaternion.identity);

}
void Wave3()
{
    Debug.Log("Wave3");
    delay = 4f;

    Instantiate(mediumFlame, new Vector3(drop1, dropHeight, 0), Quaternion.identity);
    Instantiate(mediumFlame, new Vector3(drop2, dropHeight, 0), Quaternion.identity);
    Instantiate(mediumFlame, new Vector3(drop3, dropHeight, 0), Quaternion.identity);

}
void Wave4()
{
    Debug.Log("Wave4");
    delay = .5f;


    Instantiate(smallFlame, new Vector3(drop1 - dropChange, dropHeight + dropChange, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop1, dropHeight, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop1 + dropChange, dropHeight + dropChange, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop2 - dropChange, dropHeight + dropChange, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop2, dropHeight, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop2 + dropChange, dropHeight + dropChange, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop3 - dropChange, dropHeight + dropChange, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop3, dropHeight, 0), Quaternion.identity);
    Instantiate(smallFlame, new Vector3(drop3 + dropChange, dropHeight + dropChange, 0), Quaternion.identity);

}
void Start(){
waveActive=false;
落差高度=100;
}
//每帧调用一次更新
无效更新()
{
如果(waveActive==false)
{
启动例程(SelectWave());
}
}
IEnumerator SelectWave()
{
waveActive=true;
浮点值=随机值;
如果(值25和值5和值75)
{
调用重复(“Wave4”,延迟,延迟);
}
收益率返回新的WaitForSeconds(随机范围(20f,30f));
取消调用();
waveActive=false;
}
void Wave1()
{
Debug.Log(“Wave1”);
延迟=.5f;
实例化(smallFlame,新矢量3(drop1,dropHeight,0),四元数.identity);
实例化(smallFlame,新向量3(drop2,dropHeight,0),四元数.identity);
实例化(smallFlame,新向量3(drop3,dropHeight,0),四元数.identity);
}
void Wave2()
{
Debug.Log(“Wave2”);
延时=1f;
实例化(largeFlame,新向量3(drop1,dropHeight,0),四元数.identity);
实例化(largeFlame,新向量3(drop2,dropHeight,0),四元数.identity);
实例化(largeFlame,新向量3(drop3,dropHeight,0),四元数.identity);
}
void Wave3()
{
Debug.Log(“Wave3”);
延迟=4f;
实例化(mediumFlame,新向量3(drop1,dropHeight,0),四元数.identity);
实例化(mediumFlame,新向量3(drop2,dropHeight,0),四元数.identity);
实例化(mediumFlame,新向量3(drop3,dropHeight,0),四元数.identity);
}
void Wave4()
{
Debug.Log(“Wave4”);
延迟=.5f;
实例化(smallFlame,新矢量3(drop1-dropChange,dropHeight+dropChange,0),四元数.identity);
实例化(smallFlame,新矢量3(drop1,dropHeight,0),四元数.identity);
实例化(smallFlame,新矢量3(drop1+dropChange,dropHeight+dropChange,0),四元数.identity);
实例化(smallFlame,新矢量3(drop2-dropChange,dropHeight+dropChange,0),四元数.identity);
实例化(smallFlame,新向量3(drop2,dropHeight,0),四元数.identity);
实例化(smallFlame,新矢量3(drop2+dropChange,dropHeight+dropChange,0),四元数.identity);
实例化(smallFlame,新矢量3(drop3-dropChange,dropHeight+dropChange,0),四元数.identity);
实例化(smallFlame,新向量3(drop3,dropHeight,0),四元数.identity);
实例化(smallFlame,新矢量3(drop3+dropChange,dropHeight+dropChange,0),四元数.identity);
}

与您的问题无关:到处实例化!请查看对象池。这太多了。我将对此进行研究。我不确定,但我认为当您在协同程序中使用Invoke时,Unity可怕的后台进程会被打乱。事情是这样的-协同程序创建了自己的线程,并且可能,仅仅是说,从后台线程到主线程的调用会导致您的问题(可能)。尝试使用计时器而不是协程,或者选项2:使Wave()函数也协程,这样就不必调用它们。生活以神秘的方式运作…嗨,尼卡。统一中没有“背景过程”。它绝对是单线程的。蒂姆,你几乎肯定不应该在这里使用Update()。所呈现的代码非常奇怪-请解释您正在尝试执行的操作。