[Unity]WaitForSeconds(float.NaN)的异常表现

2019/11 01 16:11

记Unity2018.4协程Coroutine的WaitForSeconds(float.Nan)的异常表现

一、异常协程体:

public class ExceptionCorout : MonoBehaviour
{
    IEnumerator WaitTask()
    {
        var c = 1.0f;
        var d = c - c;
        var f = 0 / d;
        yield return new WaitForSeconds(f);
    }
    void Start()
    {
        StartCoroutine(WaitTask());
    }
    public static void Execute()
    {
        GameObject go = new GameObject("go");
        go.AddComponent<ExceptionCorout>();
    }
}

以下代码执行必报错

float.NaN has been passed into WaitForSeconds which will result in an infinite wait time.
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
<Fire>c__Iterator1:MoveNext() (at Assets/ExceptionCorout.cs:20)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

然后后续创建的脚本,均不再执行Start()函数体

五、结论
WaitForSeconds,传入无效Nan参数,会堵住整个协程系统