UnityでCoroutineを使用しているときに奇妙な問題が発生しました。変更する前に、私のコードは次のとおりです。Unity:ArgumentException:値が期待した範囲内に収まらない
IEnumerator Destory()
{
yield return new WaitForSeconds(destoryDelay);
yield return StartCoroutine(Timer.Start(0.5f, false, gameManager.EnableBtnSummon));
GameObject.Destroy(this.gameObject);
}
Time.Start()
は自分自身によって書かれ、遅延呼び出しのために使用されるユーティリティです。 Time.Start()
はWaitForSeconds()
が含まれるため、以下のように私は上記のコードを変更することを決め
public static IEnumerator Start(float duration, bool repeat, Action callback)
{
do
{
yield return new WaitForSeconds(duration);
if (callback != null)
callback();
} while (repeat);
}
ので:
残念ながらIEnumerator Destory()
{
//yield return new WaitForSeconds(destoryDelay);
yield return StartCoroutine(Timer.Start(destoryDelay+0.5f, false, gameManager.EnableBtnSummon));
GameObject.Destroy(this.gameObject);
}
は、コンソールにはエラーをスロー:
ArgumentException: Value does not fall within the expected range.
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();
}
私はそれを理解できません。 THX ...
あなたの質問にコードを書式設定してみてください。また、このエラーはどこにありますか? 'gameManager'はどのように宣言されていますか? – Programmer
引数を分離して問題の原因を調べることができればうれしいでしょう。それぞれをローカル変数として宣言し、 'Debug.Log'を呼び出して値を送信してから送信してください。 – MrDos
コメントと修正をいただきありがとうございます。StackOverflowを初めて使用したので、あいまいになる可能性があります。 –