- Home /
IndexOutOfRangeException
I don't know how to fix this problem. It won't let me start the game immediately which can be an issue. I have these two scripts. This one has the issue at line 67-
public class BaseCharacter : MonoBehaviour {
private string _name;
private int _level;
private uint _freeExp;
private Attribute[] _primaryAttribute;
private Vital[] _vital;
private Skill[] _skill;
public void Awake()
{
_name = string.Empty;
_level = 0;
_freeExp = 0;
_primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
_vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
_skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
SetupPrimaryAttributes ();
SetupVitals ();
SetupSkills ();
}
public string Name {
get{ return _name; }
set{ _name = value; }
}
public int Level {
get{ return _level; }
set{ _level = value; }
}
public uint FreeExp {
get{ return _freeExp; }
set{ _freeExp = value; }
}
public void AddExp(uint exp) {
_freeExp += FreeExp;
CalculateLevel ();
}
public void CalculateLevel() {
}
private void SetupPrimaryAttributes(){
for (int cnt = 0; cnt < _primaryAttribute.Length; cnt++) {
_primaryAttribute [cnt] = new Attribute ();
}
}
private void SetupVitals() {
for (int cnt = 0; cnt < _vital.Length; cnt++) {
_primaryAttribute [cnt] = new Attribute ();
}
}
private void SetupSkills() {
for (int cnt = 0; cnt < _skill.Length; cnt++) {
_primaryAttribute [cnt] = new Attribute ();
}
}
public Attribute GetPrimaryAttribute(int index){
return _primaryAttribute [index];
}
public Vital GetVital(int index){
return _vital [index];
}
public Skill GetSkill(int index){
return _skill [index];
}
private void SetupVitalModifiers() {
//health
ModifyingAttribute healthModifier = new ModifyingAttribute();
healthModifier.atttribute = GetPrimaryAttribute((int)AttributeName.Constitution);
healthModifier.ratio = .5f;
GetVital ((int)VitalName.Health).AddModifier(healthModifier);
//stamina
ModifyingAttribute staminaModifier = new ModifyingAttribute();
staminaModifier.atttribute = GetPrimaryAttribute((int)AttributeName.Constitution);
staminaModifier.ratio = 1;
GetVital ((int)VitalName.Stamina).AddModifier(staminaModifier);
//mana
ModifyingAttribute manaModifier = new ModifyingAttribute();
manaModifier.atttribute = GetPrimaryAttribute((int)AttributeName.Willpower);
manaModifier.ratio = 1;
GetVital ((int)VitalName.Mana).AddModifier(manaModifier);
}
private void SetupSkillModifiers(){
//Attack
ModifyingAttribute AttackModifier1 = new ModifyingAttribute ();
ModifyingAttribute AttackModifier2 = new ModifyingAttribute ();
AttackModifier1.atttribute = GetPrimaryAttribute ((int)AttributeName.Authority);
AttackModifier1.ratio = .33f;
AttackModifier2.atttribute = GetPrimaryAttribute ((int)AttributeName.Willpower);
AttackModifier2.ratio = .33f;
GetSkill ((int)SkillName.Attack).AddModifier (AttackModifier1);
GetSkill ((int)SkillName.Attack).AddModifier (AttackModifier2);
//Defense
ModifyingAttribute DefenseModifier1 = new ModifyingAttribute ();
ModifyingAttribute DefenseModifier2 = new ModifyingAttribute ();
DefenseModifier1.atttribute = GetPrimaryAttribute ((int)AttributeName.Durability);
DefenseModifier1.ratio = .33f;
DefenseModifier2.atttribute = GetPrimaryAttribute ((int)AttributeName.Speed);
DefenseModifier2.ratio = .33f;
GetSkill ((int)SkillName.Defense).AddModifier (DefenseModifier1);
GetSkill ((int)SkillName.Defense).AddModifier (DefenseModifier2);
//Magic
ModifyingAttribute MagicModifier1 = new ModifyingAttribute ();
ModifyingAttribute MagicModifier2 = new ModifyingAttribute ();
MagicModifier1.atttribute = GetPrimaryAttribute ((int)AttributeName.Spirit);
MagicModifier1.ratio = .33f;
MagicModifier2.atttribute = GetPrimaryAttribute ((int)AttributeName.Concentration);
MagicModifier2.ratio = .33f;
GetSkill ((int)SkillName.Magic).AddModifier (MagicModifier1);
GetSkill ((int)SkillName.Magic).AddModifier (MagicModifier2);
//Archery
ModifyingAttribute ArcheryModifier1 = new ModifyingAttribute ();
ModifyingAttribute ArcheryModifier2 = new ModifyingAttribute ();
ArcheryModifier1.atttribute = GetPrimaryAttribute ((int)AttributeName.Concentration);
ArcheryModifier1.ratio = .33f;
ArcheryModifier2.atttribute = GetPrimaryAttribute ((int)AttributeName.Speed);
ArcheryModifier2.ratio = .33f;
GetSkill ((int)SkillName.Archery).AddModifier (ArcheryModifier1);
GetSkill ((int)SkillName.Archery).AddModifier (ArcheryModifier2);
}
public void StatUpdate(){
for (int cnt = 0; cnt < _vital.Length; cnt++)
_vital [cnt].Update ();
for (int cnt = 0; cnt < _skill.Length; cnt++)
_skill [cnt].Update ();
}
}
and when I created this script, I started getting the error-
using UnityEngine;
using System.Collections;
using System;
public class CharacterGenerator : MonoBehaviour {
private PlayerCharacter _toon;
//Use this for initialization
void Start() {
_toon = new PlayerCharacter ();
_toon.Awake ();
}
//Update is called once per frame
void Update() {
}
void OnGUI() {
GUI.Label(new Rect(10, 10, 50, 25), "Name:");
_toon.Name = GUI.TextArea (new Rect(65, 10, 100, 25), _toon.Name);
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
GUI.Label (new Rect(10, 40, 100, 25), ((AttributeName)cnt).ToString () );
}
}
}
I hope someone can help me! Thanks!
Adding a copy of the error message to your question would be helpful. In particular the error message will include the stack trace, so it will show which one of the many calls to GetPrimaryAttribute() is generating the error. That in turn will allow you to check the array to make sure it is initialized for the index value for that attribute.
IndexOutOfRangeException: Array index is out of range. (wrapper stelemref) object:stelemref (object,intptr,object) BaseCharacter.SetupSkills () (at Assets/Scripts/Character Classes/BaseCharacter.cs:67) BaseCharacter.Awake () (at Assets/Scripts/Character
Sorry totally forgot to! XD
You should also probably only post relevant code, as deter$$anonymous$$ed by the error message.
Answer by robertbu · Apr 19, 2014 at 09:30 PM
Your problem is on line 64 and/or line 57. Since you did not include the first few lines of your script, the line number you are using for your error messages are off. On both of those two lines you are cycling through a for() loop based on the size of the _vital and the _skill arrays, but you are indexing into the _primaryAttribute array. If the size of the _skill array is larger than the size of the _primaryAttribute array, you will get an out of range error. My guess is that those two lines of code should really be initializing the entries for the _skill and _vital arrays and not the _primaryAttribute array.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multiple Cars not working 1 Answer
NullReferenceException 1 Answer
Index out of Range Exception Error 2 Answers