Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Nsingh13 · May 18, 2015 at 12:24 AM · androiderroraipathfindingargumentoutofrangeexception

Argument out of Range Exception Error - Parameter Name: Index

Hey guys, so i keep on getting this error for my enemy AI script (which basically uses A* pathfinding to move towards a target)

My error is on this line:

Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;

This is the whole script:

 using UnityEngine;
 using Pathfinding;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(Seeker))]
 
 public class EnemyAI : MonoBehaviour
 {
 
         //what to chase
         Transform target;
 
         //restart path
         public bool restartpath = false;
 
         //How many times each second we will update our path
         public float updateRate = 2f;
 
         private Seeker seeker;
         private Rigidbody2D rb;
 
         //the calculated path
         public Path path;
 
         //AI's speed/second
         public float speed = 300f;
 
         public ForceMode2D fmode;
 
         //Waypoint we are currently moving towards
         private int currentWaypoint = 0;
 
         [HideInInspector]
         public static bool
                 pathisEnded = false;
 
         //Max distance from AI to a waypoint for it to continue to the next waypoint
         public float nextWaypointDistance = 3;
 
         void Start ()
         {
                 seeker = GetComponent<Seeker> ();
                 rb = GetComponent<Rigidbody2D> ();
                 target = GameObject.Find ("Roti").GetComponent<Transform> ();
 
                 if (target == null) {
                         //search for target
                 }
 
                 seeker.StartPath (transform.position, target.position, OnPathComplete);
     
                 StartCoroutine (UpdatePath ());
     
         }
 
         IEnumerator UpdatePath ()
         {
                 if (target == null) {
                         //search for target
 
                         return false;
                 }
 
                 seeker.StartPath (transform.position, target.position, OnPathComplete);
 
                 yield return new WaitForSeconds (1f / updateRate);
 
                 StartCoroutine (UpdatePath ());
         }
 
         public void OnPathComplete (Path p)
         {
 
                 if (!p.error) {
                         path = p;
                         currentWaypoint = 0;
                 }
         }
 
 
         void FixedUpdate ()
         {
                 if (target == null) {
                         //search for target
             
                         return;
                 }
 
                 //Optional: Always look at player?
                 Vector3 diff = target.position - transform.position;
                 diff.Normalize ();
 
                 float rot_z = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;
                 transform.rotation = Quaternion.Euler (0f, 0f, rot_z - 90);
         
 
 
                 if (path == null) {
                         return;
                 }
 
                 //end of path
                 if (currentWaypoint > path.vectorPath.Count) {
                         if (pathisEnded)
                                 return;
 
                         pathisEnded = true;
                 }
 
         if (pathisEnded == false) {
                         //Direction to next waypoint
                         Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
 
                         dir *= speed * Time.fixedDeltaTime;
 
                         //Move the AI
                         rb.AddForce (dir, fmode);
 
                         float dist = Vector3.Distance (transform.position, path.vectorPath [currentWaypoint]);
 
                         if (dist < nextWaypointDistance) {
                                 currentWaypoint++;
                                 return;
                         }
                 }
                 
         }
 
 
 }

Thanks guys! :). Any help is appreciated!

Comment
Add comment · Show 1
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 Nsingh13 · May 18, 2015 at 12:27 AM 0
Share

Note: I only get this error when my enemy is close to the target (so i believe it has no more waypoints to go to)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by hanger · May 18, 2015 at 05:21 AM

Pretty easy to miss.

 if (currentWaypoint > path.vectorPath.Count) {

You mean:

 if (currentWaypoint >= path.vectorPath.Count) {
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 Nsingh13 · May 19, 2015 at 05:42 AM 0
Share

Thank you!

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

21 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

Related Questions

The generic list blues (Argument out of range error) 1 Answer

Error building player when run on Asus Transformer Prime with android 4.0.3 3 Answers

Have falling object exit from a collider after collision? 2 Answers

Android:Install update for published app 1 Answer

Android build problem! 1 Answer


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