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 iiirwnsyh · Jun 27, 2019 at 03:49 PM · indexoutofrangeexception

IndexOutOfRangeException: Array index is out of range.

Hey all! So halfway into development of this game, I got this error that I just can't solve. I'm super new to C# as well so I might have written some unnecessary super long codes that could have been done in a single line. I'm getting there.. hopefully. I've been tested for a day. sometime it run perfectly but sometime it gaves me errors. But i guess, there is nothing wrong for code. What i did is this.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Unit : MonoBehaviour {
 
     public GameObject Abintang;
     public Pathfinding pathfinding;
 
     public Transform target;
     private float speed = 5;
     private Vector3[] path;
     private int targetIndex;
     private bool checkSet;
 
 
     public bool pathPending{
         get{ 
             return pathfinding.requestManager.isProcessingPath;
             //return m_pathPending;
         }
     }
 
     public bool hasPath{
         get{
             return pathfinding.m_hasPath;
         }
     }
 
     public Vector3 destination{
         get{ 
             if (pathfinding.requestManager.isProcessingPath){
                 return pathfinding.m_destination;
             }
             if (path.Length == 0){
                 return transform.position;
             }
             if (pathfinding.grid == null){
                 return Vector3.positiveInfinity;
             }
             return pathfinding.m_destination;
         }
         set{ 
             if (target != null) {
                 pathfinding.m_destination = target.position;
             }
             pathfinding.m_destination = value;
         }
     }
 
     public float remainingDistance{
         get{ 
             if (pathfinding.m_destination != null){
                 return Vector3.Distance( pathfinding.m_destination , transform.position );
             }
         }
     }
 
 
     public bool isStopped{
         get{
             return pathfinding.m_isStopped;
         }
         set{ 
             pathfinding.m_isStopped = value;
         }
     }
 
     public float stoppingDistance { 
         get {
             return pathfinding.m_stoppingDistance;
         } 
         set {
             if (isStopped) {
                 pathfinding.m_stoppingDistance = Vector3.Distance (pathfinding.m_destination, transform.position);
             }
         }
     }
 
     void Start(){
         pathfinding = Abintang.GetComponent<Pathfinding> ();
         //PathRequestManager.RequestPath (transform.position, target.position, OnPathFound);
     }
 
     void Update(){
         if (checkSet) {
             PathRequestManager.RequestPath (transform.position, pathfinding.m_destination , OnPathFound);
         }
 
 
         if (target != null) {
             if (Vector3.Distance (transform.position, target.position) > 1f) {
                 SetDestination (target.position);
             }
         }
     }
 
     public void OnPathFound(Vector3[] newPath,bool pathSuccessful){
         if (pathSuccessful) {
             path = newPath;
             Debug.Log ("Path Length = " + path.Length.ToString ());
             StopCoroutine ("FollowPath");
             StartCoroutine ("FollowPath");
         }
     }
 
     IEnumerator FollowPath(){
         Vector3 currentWaypoint = path [0];
         targetIndex = 0;
 
         while (true) {
             if (transform.position == currentWaypoint) {
                 targetIndex++;
                 Debug.Log ("Target Index = " + targetIndex.ToString ());
                 if (targetIndex >= path.Length) {
                     yield break;
                 }
                 currentWaypoint = path [targetIndex];
             }
             transform.LookAt (currentWaypoint);
             print ("current waypoint : " + currentWaypoint.ToString ());
             transform.position = Vector3.MoveTowards (transform.position, currentWaypoint, speed * Time.deltaTime);
             checkSet = false;
             //Debug.Log ("CheckSet = False");
 
             yield return null;
         }
     }
 
     public void OnDrawGizmos(){
         if (path != null) {
             for (int i = targetIndex; i < path.Length; i++) {
                 Gizmos.color = Color.black;
                 Gizmos.DrawCube (path [i], Vector3.one);
 
                 if (i == targetIndex) {
                     Gizmos.DrawLine (transform.position, path [i]);
                 } else {
                     Gizmos.DrawLine (path [i - 1], path [i]);
                 }
             }
         }
     }
 
     public bool SetDestination( Vector3 _target){
         if (_target != null) {
             pathfinding.m_destination = _target;
             checkSet = true;
             Debug.Log ("CheckSet = true");
             print ("SetDestination = " + _target.ToString());
             return true;
         } else {
             checkSet = false;
             return false;
         }
     }
 
     public void Stop(){
         StopCoroutine ("FollowPath");
         pathfinding.m_isStopped = true;
     }
 
     public void Resume(){
         PathRequestManager.RequestPath (transform.position, pathfinding.m_destination, OnPathFound);
         //StopCoroutine ("FollowPath");
         //StartCoroutine ("FollowPath");
         pathfinding.m_isStopped = false;
     }
     public void HasReachedDestination(){
         if (Vector3.Distance (transform.position, pathfinding.m_destination) < 0.10f) {
             checkSet = false;
             Debug.Log ("CheckSet = False");
         }
     }
 
     public void ResetPath(){
         System.Array.Resize (ref path, 0);
     }
 }
 

and the error point to this line

 Vector3 currentWaypoint = path [0];

Thanks for taking out the time to help me out! :)

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

2 Replies

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

Answer by Hellium · Jun 27, 2019 at 03:58 PM

Add the following lines as the first lines of the FollowTarget function in order to avoid the error

  IEnumerator FollowPath(){
      if( path.Length == 0 )
           yield break ;

      Vector3 currentWaypoint = path [0];
      targetIndex = 0;
Comment
Add comment · 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
0

Answer by fffMalzbier · Jun 27, 2019 at 04:02 PM

Out of range means you tried to access a index of a array that does not exist.

You debug code should have shown you that the path does not have any length. (length = 0) Trying to access index 0 does not work in that case. Try to figure out why the path did not have any points and try to prevent the function to start in case the path is not valid.

Comment
Add comment · 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

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

178 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 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

Unity C# array index is out of range 1 Answer

Minesweeper UI Issue 0 Answers

IndexOutOfRangeException: Index was outside the bounds of the array. 0 Answers

IndexOutOfRangeException: Array index is out of range. SceneSwitcher.OnGUI () (at Assets/Packages/Curvy Examples/Scripts/SceneSwitcher.cs:125) 1 Answer

Index Out Of Range Exception When Spawning Second Wave 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