C# Unity:ArgumentException:值不在预期范围内

C# Unity:ArgumentException:值不在预期范围内,c#,unity3d,C#,Unity3d,我在Unity中使用协同程序时遇到了一个奇怪的问题。修改前,我的代码如下: IEnumerator Destory() { yield return new WaitForSeconds(destoryDelay); yield return StartCoroutine(Timer.Start(0.5f, false, gameManager.EnableBtnSummon)); GameObject.Destroy(this.gameObject); } IEnum

我在Unity中使用协同程序时遇到了一个奇怪的问题。修改前,我的代码如下:

IEnumerator Destory()
{
    yield return new WaitForSeconds(destoryDelay);
    yield return StartCoroutine(Timer.Start(0.5f, false, gameManager.EnableBtnSummon));
    GameObject.Destroy(this.gameObject);
}
IEnumerator Destory()
{
    //yield return new WaitForSeconds(destoryDelay);
    yield return StartCoroutine(Timer.Start(destoryDelay+0.5f, false, gameManager.EnableBtnSummon));
    GameObject.Destroy(this.gameObject);
}
Time.Start()
是我自己编写的用于延迟调用的实用程序

public static IEnumerator Start(float duration, bool repeat, Action callback)
{
    do
    {
        yield return new WaitForSeconds(duration);                
        if (callback != null)
            callback();
    } while (repeat);
}
因为
Time.Start()
包含
WaitForSeconds()
,所以我决定修改上述代码如下:

IEnumerator Destory()
{
    yield return new WaitForSeconds(destoryDelay);
    yield return StartCoroutine(Timer.Start(0.5f, false, gameManager.EnableBtnSummon));
    GameObject.Destroy(this.gameObject);
}
IEnumerator Destory()
{
    //yield return new WaitForSeconds(destoryDelay);
    yield return StartCoroutine(Timer.Start(destoryDelay+0.5f, false, gameManager.EnableBtnSummon));
    GameObject.Destroy(this.gameObject);
}
很遗憾,console会抛出一个错误:

ArgumentException:值不在预期范围内

gameManager.EnableBtnSummon
只是一个动作处理游戏逻辑。调试后,我确保在运行此函数之前发生错误。但我会展示给大家更多的线索

public void EnableBtnSummon()
{
    //will not reach this!
    print("Enable Button Summon");
    //if detecting monster, change relative sprite of monster medal
    if (currentMonsterIndex != -1)
    {
        Image captureMonsterSprite = monsterMedalList.transform.GetChild(currentMonsterIndex).GetComponent<Image>();
        captureMonsterSprite.sprite = mosnterExplicitMedalList[currentMonsterIndex];

        Image gameOverMonsterSprite = gameOverMonsterList.transform.GetChild(currentMonsterIndex).GetComponent<Image>();
        gameOverMonsterSprite.sprite = mosnterExplicitMedalList[currentMonsterIndex];

        currentMonsterIndex = -1;
        captureMonsterCount++;
    }

    if (captureMonsterCount == monsterIndexDictionary.Count) return;

    var summonAnimator = btnSummon.GetComponent<Animator>();
    summonAnimator.SetBool("isSearch", false);

    btnSummon.enabled = true;
    btnExit.enabled = true;

    fogParticleSystem.Play();
}
public void EnableBtnSummon()
{
//不会达到这个目标的!
打印(“启用按钮召唤”);
//如果检测到怪物,则更改怪物奖章的相对精灵
如果(当前索引!=-1)
{
Image captureMonsterSprite=monsterMedalList.transform.GetChild(currentMonsterIndex.GetComponent();
captureMonsterSprite.sprite=mosinterexplicitMedallist[currentMonsterIndex];
Image gameOverMonsterSprite=gameOverMonsterList.transform.GetChild(currentMonsterIndex.GetComponent();
gameOverMonsterSprite.sprite=mosinterexplicitmedallist[currentMonsterIndex];
当前指数=-1;
captureMonsterCount++;
}
如果(captureMonsterCount==monsterIndexDictionary.Count)返回;
var callowanimator=btnSummon.GetComponent();
召唤动画师.SetBool(“isSearch”,false);
btnSummon.enabled=true;
btnExit.enabled=true;
fogParticleSystem.Play();
}
我不明白,有人能告诉我发生了什么吗?thx…

例外情况:

ArgumentException:值不在预期范围内

发生在这行代码上:

yield return StartCoroutine(MoveTowards.Start(destoryDelay + 0.5f, false, gameManager.EnableBtnSummon));

正如问题的标题所说,这与启动例行程序无关。问题的根源是
moveToward.Start
corroutine函数。传递给它的第三个参数(
操作回调
)是问题

问题是您正在将
null
传递给
moveToward.Start
函数的第三个参数。由于您正在将
gameManager.EnableBtnSummon
传递给第三个参数,这意味着
gameManager
变量为
null

您可以通过在代码行之前添加
Debug.Log(gameManager)
来验证这一点。控制台选项卡中的输出应为“null”

修复:

初始化
gameManager
变量:

命名
GameObject
您的
GameManager
脚本附加到“ManagerObj”,然后使用下面的简单代码初始化GameManager变量

GameManager gameManager;

void Awake()
{
    gameManager = GameObject.Find("ManagerObj").GetComponent<GameManager>();
}
GameManager GameManager;
无效唤醒()
{
gameManager=GameObject.Find(“ManagerObj”).GetComponent();
}
注:

Start
函数重命名为其他函数,因为已经有名为“Start”和“Awake”的统一内置函数。您需要将名称更改为其他名称,但这不是问题所在。 例外情况:

ArgumentException:值不在预期范围内

发生在这行代码上:

yield return StartCoroutine(MoveTowards.Start(destoryDelay + 0.5f, false, gameManager.EnableBtnSummon));

正如问题的标题所说,这与启动例行程序无关。问题的根源是
moveToward.Start
corroutine函数。传递给它的第三个参数(
操作回调
)是问题

问题是,您正在将
null
传递到
movedorn.Start
函数的第三个参数。由于您正在将
gameManager.EnableBtnSummon
传递给第三个参数,这意味着
gameManager
变量为
null

您可以通过在代码行之前添加
Debug.Log(gameManager)
来验证这一点。控制台选项卡中的输出应为“null”

修复:

初始化
gameManager
变量:

命名
GameObject
您的
GameManager
脚本附加到“ManagerObj”,然后使用下面的简单代码初始化GameManager变量

GameManager gameManager;

void Awake()
{
    gameManager = GameObject.Find("ManagerObj").GetComponent<GameManager>();
}
GameManager GameManager;
无效唤醒()
{
gameManager=GameObject.Find(“ManagerObj”).GetComponent();
}
注:

Start
函数重命名为其他函数,因为已经有名为“Start”和“Awake”的统一内置函数。您需要将名称更改为其他名称,但这不是问题所在。

请尝试格式化问题中的代码。还有,你从哪里得到这个错误?
gameManager
是如何声明的?如果您可以隔离您的参数,看看是哪一个导致了问题,那就太好了。您应该将每个变量声明为局部变量,然后在发送它们之前调用
Debug.Log
来验证它们的值。感谢您的评论和修改,这是我第一次使用StackOverflow,因此它可能不明确。欢迎使用so。您可以使用@username回复特定用户。我问如何声明
gameManager
EnableBtnSummon
。如果你不介意展示的话,那就太棒了。也许把课程本身贴出来。。。。这些都是用来说明发生了什么的。@Programmer再次感谢!我已经更新了我的问题,但我认为
gameManager
并不重要,因为在调用操作之前就发生了错误。请尝试格式化问题中的代码。还有,你从哪里得到这个错误?
gameManager
是如何声明的?如果您可以隔离您的参数,看看是哪一个导致了问题,那就太好了。您应该将每个变量声明为局部变量,然后在发送它们之前调用
Debug.Log
来验证它们的值。感谢您的评论和修改,这是我第一次使用StackOverflow,因此它可能不明确。Welcom