Why it throws "Index is out of range" Exception here?
Hi, Everyone, I've been working on a kind of AI for my game and once I finished everything and tested, it worked. However, sometimes it throws an unexpected exception which says ("Array Index Is Out Of Range" c__Iterator0.MoveNext() at opponentAI.cs:92).
Here's my code exactly starting at line 92:
while (Vector3.Distance(opponentCards[i].transform.position, AIDropZone.transform.position) > 0.2f)
{
StartCoroutine(MoveToPosition(opponentCards[i].gameObject, AIDropZone.transform.position, step));
isDone = true;
yield return null;
}
I got to mention also that the exception only occurs when MoveToPosition() lerp interpolation is 1 or close to it and after that block of code there's also an IF statement that I added trying to get over this exception
if(Vector3.Distance(opponentCards[i].transform.position, AIDropZone.transform.position) <= 0.2f)
{
AIDropZoneScript.ArrivedAtDropZone(opponentCards[i].gameObject);
StartCoroutine(controller.ResetTimer());
break;
}
I believe StartCoroutine() is causing this exception but I'm not sure why and how to fix it considering the fact that I need MoveToPosition() to finish before calling the IF afterwards. PS: ArrivedAtDropZone() must be called after finishing the MoveToPosition() Coroutine.
Here's all the code from AIDropZone.ArrivedAtDropZone():
public void ArrivedAtDropZone(GameObject opCard)
{
Transform child;
opCard.gameObject.tag = "cardsWon";
opCard.gameObject.GetComponent<SpriteRenderer>().sprite = cardBackFace;
opCard.transform.position = transform.position;
opCard.gameObject.transform.parent = transform;
for (int i = 0; i < opCard.transform.childCount; i++)
{
child = opCard.gameObject.transform.GetChild(i);
child.tag = "cardsWon";
child.GetComponent<SpriteRenderer>().sprite = cardBackFace;
child.transform.parent = transform;
}
}
PS: Used a lot of Coroutine to keep the whatever action/code active after frame updates (needed for AI)
Any help is highly appreciated !
Answer by Cuttlas-U · Oct 02, 2017 at 04:04 PM
hi; this problem i mean "Index is out of range" always happens when u are trying to access an element of the array while the array dont have that element ;
like u want to access the element number 5 of the array but the array length is just 3 ;
so to fix this problem u have to Debug.Log the array counter above the lane that it says it has error like this :
Debug.Log(i);
while (Vector3.Distance(opponentCards[i].transform.position, AIDropZone.transform.position) > 0.2f)
{
//
//
//
/
and in the console check the logs and also check the array length , see when console gives u a number that is not in array length u can then check how that number is producing and prevent it from being produced;
Actually, I did Debug.Log(i); and Debug.Log(j); but both seemed okay to me since the index is correct and is not empty. However, I have some thoughts that it may have done something that is supposed to happen just after the coroutine. So, I have another question, how can I stop it from doing anything next until the previous coroutine is done?