【发布时间】:2017-09-29 10:23:09
【问题描述】:
我在 Unity 中使用 Coroutine 时遇到了一个奇怪的问题。修改前,我的代码如下:
IEnumerator Destory()
{
yield return new WaitForSeconds(destoryDelay);
yield return StartCoroutine(Timer.Start(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(destoryDelay+0.5f, false, gameManager.EnableBtnSummon));
GameObject.Destroy(this.gameObject);
}
很遗憾,控制台报错:
ArgumentException: 值不在预期范围内。
gameManager.EnableBtnSummon只是一个Action处理游戏逻辑。调试后,我确保在此函数运行之前发生错误。但我会展示它以获得更多线索。
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();
}
我看不懂,谁能告诉我会发生什么?谢谢...
【问题讨论】:
-
尝试格式化您问题中的代码。另外,你从哪里得到这个错误?
gameManager是如何声明的? -
如果你能隔离你的论点来看看是哪一个导致了问题,那就太好了。您应该将每个变量声明为局部变量,然后在发送它们之前调用
Debug.Log来验证它们的值。 -
感谢cmets和修改,我是第一次使用StackOverflow,所以可能有歧义。
-
欢迎来到 SO。您可以使用@username 回复特定用户。我问
gameManager和EnableBtnSummon是如何声明的。如果你不介意展示这一点,那就太棒了。也许发布课程本身......。这些是说明发生了什么所必需的。 -
@Programmer 再次感谢!我已经更新了我的问题,但我认为
gameManager无关紧要,因为即使在调用 Action 之前就发生了错误。