Problems with IndexOutOfRangeException:

Hey guys, i need some help, when I put this script appears this error:

IndexOutOfRangeException: Array index is out of range. PlayerController.SwitchWeapon (Int32 w) (at Assets / Assets / scripts / PlayerController.cs: 81) PlayerController.Awake () (at Assets / Assets / scripts / PlayerController.cs: 39)

follows the code snippets with error, i don’t know what i have to do:

using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;


public class PlayerController : MonoBehaviour {
    //FPS CONTROLLER
    private FirstPersonController controller;

    //HOLDERS
    public Transform walkHolder;
    public Transform jumpHolder;
    public Transform swayHolder;
    public Transform recoilHolder;
    public Transform adsHolder;

    public Camera mainCamera;




    [SerializeField]
    public WeaponInfo[] allWeaponInfo;
    public WeaponInfo weapon;

    public int curWeapon;
    public int[] playerWeapons;



    private Vector3[] recoil = new Vector3[4];
    public float health;
    public bool switching;
    public int nextWeapon = -1;

    // Use this for initialization
    void Awake ()
    {
        SwitchWeapon (0);
    }

    // Update is called once per frame
    void FixedUpdate () {
        AnimationController ();
        RecoilController ();
        ADSController ();
    }

    void Update () {
        ShootController ();
    }

    void LateUpdate () {
        AmmoController ();
    }

    private void AnimationController(){
        if (Input.GetKeyDown (KeyCode.Alpha1)) {
            nextWeapon = 0;
        }


        if (Input.GetKeyDown (KeyCode.Alpha2)) {
            nextWeapon = 1;
        }

        if (nextWeapon != -1 && nextWeapon != curWeapon && switching == false) {
            switching = true;
            swayHolder.GetComponent<Animation>().Play ("WeaponDown");
        }

        if (switching && swayHolder.GetComponent<Animation> ().isPlaying == false) {
            SwitchWeapon (nextWeapon);
        }

    }

    private void SwitchWeapon(int w)
    {
        curWeapon = w;
        weapon = allWeaponInfo [playerWeapons[curWeapon]];
        WeaponController ();
    }

The index you’re using to access either “allWeaponInfo” or “playerWeapons” is too large or too small. Which is what the error explicitly tells you…

What variables did you assign in the Editor for

  • allWeaponInfo

  • playerWeapons

Either you don’t have any values in playerWeapons, so playerWeapons[0] is invalid
or the value in playerWeapons[0] (the first value you assigned in the the editor) is a number way bigger than the number of items you put into allWeaponInfo.

You didn’t initialize the attribute allWeaponInfo so the script is trying to access to invalid memory position that doesn’t exist. You have reserve memory first with a given size:

allWeaponInfo = new int [max_size];

Now you can access from allWeaponInfo[0] to allWeaponInfo[max_size - 1]

Happy Unitying!!. Regards, Rudy

@SmartisticGames : Actually you only have to initialize it in that manner if its a private or protected field.
The OP’s code:

 [SerializeField]
    public WeaponInfo[] allWeaponInfo;

Is perfectly correct. He just has to make sure he assigns the correct size and values in the editor. It will perform the initialization (your code) in the background

Well, not perfectly :slight_smile: Making the field public and decorating it with SerializeField is redundant because Unity serializes all public fields by default. With that said, if @aleffB did not add any items to allWeaponInfo via the Inspector then attempting to get any index of that array would result in the error being reported.

1 Like

Yes but saying that the error is from not initializing the field could be misleading when all the OP had to do was make sure his assignments in the editor were correct.

Totally agree that the post you were responding to was incorrect - just wanted to point out the redundancy :slight_smile: