UnityException:子项超出范围且ArgumentOutOfRangeException

时间:2019-11-30 11:44:17

标签: c# unity3d exception coroutine

每个人。这件事使我整夜都无法入睡。所以我想我需要一些帮助。

我在玩纸牌游戏,我的代码有问题。

public class Round Session: MonoBehaviour{
     List<Transform> cardsInDeck;

    public void TakeNextCard()
    {
        activePlayer = GetMe();
        if (FindObjectOfType<MyHand>().CountCards() >= 2 ||
            cardList.Count <= 0) { return; }
        else
        {
            int randomCardNumber = GetRandomCardNumber();
            print(cardList[randomCardNumber]);
            int cardInt = cardList[randomCardNumber];

            if ( activePlayer.tag == "Me") //warunek aktywnego gracza
            {
                Run.Coroutine(FindObjectOfType<CardAnimation>().GiveCard(activePlayer, cardsInDeck.Count, cardInt));
                cardsInDeck.RemoveAt(cardsInDeck.Count - 1);
                activePlayer.addCard();
                cardList.RemoveAt(randomCardNumber);
            }
        }
    }
}

cardsInDeck存储卡座子代的所有变换。当我单击卡座对象并且调试器说cardsInDeck.Count等于2时,将调用此方法,但是随后运行协程并执行以下代码:

 public IEnumerator GiveCard(Player player, int childNumber, int cardNumber)
    {
        Debug.Log("In CardAnimation " + childNumber);
        Transform card = deck.GetChild(childNumber-1);
        if (player.tag == "Me")
        {
            Card drawnCard = null;
            Tween tween = card.DOLocalMove(new Vector3(350.0f, -235.0f, 0.0f), 1.0f).SetEase(Ease.InQuad);
            yield return tween.WaitForCompletion();
            drawnCard = Instantiate(cardPrefab, screenOverlayCanvas);
            drawnCard.SetCurrentCardNumber(cardNumber);
            Destroy(card.gameObject);
            drawnCard.transform.localPosition = new Vector3(378.1f, -313.8f, 0.0f);
            drawnCard.transform.localEulerAngles = new Vector3(12f, -16.17f, 0.0f);
            drawnCard.transform.localScale = new Vector3(2.04f, 1.5f, 1.5f);
            Sequence seq = DOTween.Sequence();
            seq.Append(drawnCard.transform.DOLocalMove(new Vector3(162f, -540f, 0f), 1.0f).SetEase(Ease.InQuad));
            seq.Join(drawnCard.transform.DOLocalRotate(new Vector3(180f, 0f, 0f), 1.0f).SetEase(Ease.InQuad));
            if(player.GetCardOnHand() > 1)
                seq.Join(screenOverlayCanvas.GetChild(0).GetChild(0).transform.DOLocalMove(new Vector3(-29.55f, -50f, 0f), 1.0f).SetEase(Ease.InQuad));
            seq.Join(drawnCard.transform.DOScale(new Vector3(3f, 3f, 3f), 1.0f).SetEase(Ease.InQuad)).OnComplete(() => {
                Destroy(card.gameObject);
                drawnCard.transform.SetParent(screenOverlayCanvas.GetChild(0));
                player.SetCardOnHand(cardNumber);
            });
        }
        else
        {
            if (player.tag == "Left Opponent")
            {
                Sequence seq = DOTween.Sequence();
                seq.Append(card.DOMove(player.transform.position, 1.0f).SetEase(Ease.InQuad));
                seq.Join(card.DOLocalRotate(new Vector3(25f, -10f, -84f), 1.0f).SetEase(Ease.OutQuad)).OnComplete(() => {
                    Destroy(card.gameObject);
                    player.SetCardOnHand(cardNumber);
                });
                yield return seq.WaitForCompletion();
            }

            else if (player.tag == "Center Opponent")
            {
                Sequence seq = DOTween.Sequence();
                seq.Append(card.DOMove(new Vector3(player.transform.position.x, player.transform.position.y + 0.5f, player.transform.position.z),
                    1.0f).SetEase(Ease.InQuad));
                seq.Join(card.DOLocalRotate(new Vector3(-8f, 0f, -180f), 1.0f).SetEase(Ease.OutQuad)).OnComplete(() => {
                    Destroy(card.gameObject);
                    player.SetCardOnHand(cardNumber);
                });

                yield return seq.WaitForCompletion();
            }
            else if (player.tag == "Right Opponent")
            {

                Sequence seq = DOTween.Sequence();
                seq.Append(card.DOMove(player.transform.position, 1.0f).SetEase(Ease.InQuad));
                seq.Join(card.DOLocalRotate(new Vector3(15f, 0f, 90f), 1.0f).SetEase(Ease.OutQuad)).OnComplete(() => {
                    Destroy(card.gameObject);
                    player.SetCardOnHand(cardNumber);
                });
                yield return seq.WaitForCompletion();
            }
        }
}

当调用此函数时,childNumber等于0,当然在此行上:变换卡= deck.GetChild(childNumber-1);我显然会得到以下错误:UnityException:子项超出范围,并且ArgumentOutOfRangeException。

那是为什么?为什么参数的值会改变?

对于这个问题的每一次输入,我将不胜感激。

0 个答案:

没有答案