Search Unity

IndexOutOfRangeException: Array index is out of range

Discussion in 'Scripting' started by deri007, Aug 1, 2017.

  1. deri007

    deri007

    Joined:
    Oct 5, 2016
    Posts:
    1
    I have a problem in the game while running it. There was a console output error in the game.

    IndexOutOfRangeException: Array index is out of range.
    child.Patroli () (at Assest/FantasyMonster/child.cs:94)

    this is my script
    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. using UnityEngine.AI;
    6.  
    7.  
    8. public class child : MonoBehaviour {
    9.      
    10.     private NavMeshAgent agent;
    11.  
    12.     public enum State{
    13.         PATROL,
    14.         CHASE
    15.      
    16.     }
    17.  
    18.     private float verticalVelocity;
    19.  
    20.     private Vector3 dir ;
    21.     public float speed = 0.0f;
    22.     public float c= 1f;
    23.  
    24.     public State state;
    25.     public bool alive;
    26.     //var untuk patroli
    27.     public GameObject[] waypoints;
    28.     private int waypointind =0;
    29.     public float patroliSpeed = 10f;
    30.     //var untuk chase
    31.     public float chaseSpeed = 20f;
    32.     public GameObject targets; // target to aim for
    33.     public Animator anim; // target to aim for
    34.  
    35.  
    36.  
    37.  
    38.     // Use this for initialization
    39.     private void Start()
    40.     {
    41.      
    42.         // get the components on the object we need ( should not be null due to require component so no need to check )
    43.         agent = GetComponent<NavMeshAgent>();
    44.         agent.updatePosition = true;
    45.         agent.updateRotation = false;
    46.  
    47.         waypoints = GameObject.FindGameObjectsWithTag ("waypoint");
    48.         targets = GameObject.FindGameObjectWithTag ("Player");
    49.         waypointind = Random.Range (0, waypoints.Length);
    50.  
    51.         state = child.State.PATROL;
    52.      
    53.         alive = true;
    54.     }
    55.  
    56.     private void Update(){
    57.         if (Vector3.Distance (this.transform.position,targets.transform.position) > 20) {
    58.             anim.SetBool ("walk", true);
    59.             anim.SetBool ("idle", false);
    60.             anim.SetBool ("attack", false);
    61.             print ("jarak 21.90 speed:10 health:100 = patroli");
    62.             Patroli();
    63.  
    64.         }
    65.  
    66.         if (Vector3.Distance (this.transform.position,targets.transform.position) < 20) {
    67.             Chaset ();
    68.             anim.SetBool ("walk", true);
    69.             anim.SetBool ("idle", false);
    70.             anim.SetBool ("attack", false);
    71.             print ("jarak 19.15 speed:20 health:100 = kejar");
    72.             Vector3 direction = this.transform.position - targets.transform.position;
    73.  
    74.             this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0f);
    75.  
    76.             if (direction.magnitude > c) {
    77.                 anim.SetBool ("attack", true);
    78.                 anim.SetBool ("walk", false);
    79.                 anim.SetBool ("idle", false);
    80.  
    81.                 print ("jarak 3.21 speed:0 health:50 = Serang");
    82.  
    83.             }
    84.         }
    85.  
    86.         }
    87.  
    88.  
    89.  
    90.  
    91.     // Update is called once per frame
    92.     void Patroli()
    93.     {
    94.      
    95.         agent.speed = patroliSpeed;
    96.         if (Vector3.Distance (this.transform.position, waypoints[waypointind].transform.position) >= )
    97.         {
    98.             agent.SetDestination (waypoints[waypointind].transform.position);
    99.  
    100.         }
    101.         else if (Vector3.Distance (this.transform.position, waypoints[waypointind].transform.position) <= 30)
    102.         {
    103.             waypointind = Random.Range (0, waypoints.Length);
    104.         }
    105.  
    106.  
    107.  
    108.      
    109.     }
    110.  
    111.     void Chaset()
    112.     {
    113.         agent.speed = chaseSpeed;
    114.         agent.SetDestination (targets.transform.position);
    115.  
    116.  
    117.     }
    118.  
    119.  
    120.  
    121.     public void Mulai(){
    122.         StartCoroutine ("FSM");
    123.      
    124.     }
    125.  
    126.     public void Tunggu(){
    127.         StopCoroutine("FSM");
    128.     }
    129.     }
    130.  
    131.  
    132.  
    133.  
    134.  
    135.  
     
  2. Whippets

    Whippets

    Joined:
    Feb 28, 2013
    Posts:
    1,775
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    'max' in Random.Range(int min, int max) is exclusive, see documentation. Using Random.Range(0, 10) will return values between 0 and 9. 'max' is inclusive in the float variant of Random.Range only.
     
  4. Whippets

    Whippets

    Joined:
    Feb 28, 2013
    Posts:
    1,775
    Ahh, my bad on that one then XD
     
  5. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    599
    Code (CSharp):
    1. waypoints = GameObject.FindGameObjectsWithTag ("waypoint");
    2.  
    Put a breakpoint after this line. I bet you will find that no objects with that tag were found. If the array is not empty, put a breakpoint at the start of the patroli method. The objects in the array may be destroyed before you access them there.

    If you try to access any index of an empty array, you will get an out-of-range error.

    If a waypoint is ever deleted/replaced in your game, you will need to reget the objects with that tag, because the waypoints array may have had objects in it when you set the field, but it may be empty when you try to access it later.

    Perhaps just add this in the patroli method:

    Code (CSharp):
    1. if(waypoints.Length == 0) {
    2.    waypoints = GameObject.FindGameObjectsWithTag("waypoint");
    3. }