[SOLVED]IndexOutOfRangeException: Array index is out of range Error

Hey guys,

So iv been working on a game and ran into the following error which for the life of me I cant seem to figure out.

IndexOutOfRangeException: Array index is out of range.
StatAllocationModule.DisplayStatToggleSwitches () (at Assets/Scripts/CreateAPlayerGUI/StatAllocationModule/StatAllocationModule.cs:16)
StatAllocationModule.DisplayStatAllocationModule () (at Assets/Scripts/CreateAPlayerGUI/StatAllocationModule/StatAllocationModule.cs:11)
DisplayCreatePlayerFunctions.DisplayStatAllocation () (at Assets/Scripts/CreateAPlayerGUI/DisplayCreatePlayerFunctions.cs:49)
CreateAPlayerGUI.OnGUI () (at Assets/Scripts/CreateAPlayerGUI/CreateAPlayerGUI.cs:41)

The First two errors point to this script:

using UnityEngine;
using System.Collections;

public class StatAllocationModule {

    private string[] statNames = new string[6] {"Stamina", "Endurance", "Intellect", "Strength", "Agility", "Resistance"};
    private string[] statDescription = new string[6] {"Health Modifier", "Energy Modifier", "Magical Damage Modifier", "Physical Damage modifier", "haste and critical strike modifier", "All Damage reduction"};
    private bool[] statSelections = new bool[6];

    public void DisplayStatAllocationModule(){
        DisplayStatToggleSwitches ();
    }

    private void DisplayStatToggleSwitches(){
        for(int i = 0; 1< statNames.Length; i++){
            statSelections[i] = GUI.Toggle(new Rect(10,60*i +10,100,50), statSelections[i], statNames[i]);
        }
    }

}

The 3rd error in this script:

using UnityEngine;
using System.Collections;

public class DisplayCreatePlayerFunctions {

    private StatAllocationModule statAllocationModule = new StatAllocationModule ();

    private int classSelection;
    private string[] classSelectionNames = new string[] {"Mage", "Warrior", "Archer", "Rogue", "Warlock", "Paladin"};

    public void DisplayClassSelections(){
        //A list of toggle buttons and each button will be a different class
        // selection grid
        classSelection = GUI.SelectionGrid (new Rect (50,50,250,300), classSelection, classSelectionNames, 2);
        GUI.Label (new Rect (450, 50, 300, 300), FindClassDescription (classSelection));
        GUI.Label (new Rect (450, 100, 300, 300), FindClassStatValues (classSelection));

    }

    private string FindClassDescription(int classSelection){
        if (classSelection == 0) {
            BaseCharacterClass tempClass = new BaseMageClass();
            return tempClass.CharacterClassDescription;
        }else if(classSelection == 1) {
            BaseCharacterClass tempClass = new BaseWarriorClass();
            return tempClass.CharacterClassDescription;
        }
        return "NO CLASS FOUND";
    }

    private string FindClassStatValues(int classSelection){
        if (classSelection == 0) {
            BaseCharacterClass tempClass = new BaseMageClass();
            string tempStats = "Stamina " + tempClass.Stamina + "\n" + "Endurance " + tempClass.Endurance;
            return tempStats;
        }else if(classSelection == 1) {
            BaseCharacterClass tempClass = new BaseWarriorClass();
            string tempStats = "Stamina " + tempClass.Stamina + "\n" + "Endurance " + tempClass.Endurance;
            return tempStats;
        }
        return "NO STATS FOUND";
    }



    public void DisplayStatAllocation(){
        //a list of stats with plus and minus buttons to add stats
        //logic to make sure player cannot add more than stats given
        statAllocationModule.DisplayStatAllocationModule ();
    }

    public void DisplayFinalSetup(){
        //name
        //gender
        //add a description to character
    }

    public void DisplayMainItems(){
        Transform player = GameObject.FindGameObjectWithTag ("Player").transform;
        GUI.Label(new Rect(Screen.width/2, 20, 250, 250), "CREATE NEW PLAYER");

        if(GUI.RepeatButton(new Rect(340,370,50,50), "<<<")){
            //turn transform tag player to left
            player.Rotate(Vector3.up);
        }
        if(GUI.RepeatButton(new Rect(470,370,50,50), ">>>")){
            //turn
            player.Rotate(Vector3.down);
        }



    }

}

and last but not least:

using UnityEngine;
using System.Collections;

public class CreateAPlayerGUI : MonoBehaviour {

    public enum CreateAPlayerStates{
        CLASSSELECTION, //display all class types
        STATALLOCATION, //Allocate stats where player wants to
        FINALSETUP        //Add name and misc items gender

    }

    private DisplayCreatePlayerFunctions displayFuntions = new DisplayCreatePlayerFunctions ();
    public static CreateAPlayerStates currentState;

    // Use this for initialization
    void Start () {
        currentState = CreateAPlayerStates.STATALLOCATION;
    }
   
    // Update is called once per frame
    void Update () {
        switch (currentState) {
        case(CreateAPlayerStates.CLASSSELECTION):
            break;
        case(CreateAPlayerStates.STATALLOCATION):
            break;
        case(CreateAPlayerStates.FINALSETUP):
            break;
        }
    }

    void OnGUI(){
        displayFuntions.DisplayMainItems ();
        if (currentState == CreateAPlayerStates.CLASSSELECTION) {
            //displays class selection function
            displayFuntions.DisplayClassSelections();
        }
        if (currentState == CreateAPlayerStates.STATALLOCATION) {
            //displays class selection function
            displayFuntions.DisplayStatAllocation();
        }
        if (currentState == CreateAPlayerStates.FINALSETUP) {
            //displays class selection function
            displayFuntions.DisplayFinalSetup();
        }
    }
}

Now iv had a look at it and cant seem to figure it out and im hoping an extra set of eyes might sort the problem.

for(int i =0; 1< statNames.Length; i++){

i, not 1

I guess

for(int i = 0; 1< statNames.Length; i++){

should be

for(int i = 0; i< statNames.Length; i++){

Thank you LMAO I actually just noticed it :confused:

Coding early in the morning then trying to figure out whats wrong in the afternoon is horrible…

Thanks for the help.

Classic ;D