Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by arnatus · Oct 02, 2017 at 03:35 AM · aicoroutine errorsvector3.lerpindexoutofrangeexceptionvector3.distance

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 !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image arnatus · Oct 03, 2017 at 04:38 AM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detecting Enemy Distance via List 2 Answers

Lerp to a fixed distance 0 Answers

How do I make opposing sides in a unity game 0 Answers

Implement Vector3.MoveTowards follow with random movement 0 Answers

how to search for a random point until the condition is true? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges