- Home /
Random exception error just showed up
For no reason at all i started unity today and got an out of range and exception error on a script that never had an issue before? here's my script and here the error.
ERROR :
IndexOutOfRangeException: Array index is out of range.
BaseCharacter.GetVitals (Int32 index) (at Assets/Game Data/Player Data/Scripts/Character Stats/BaseCharacter.cs:85)
GameSettings.LoadCharacterData () (at Assets/Game Data/Player Data/Scripts/Character Stats/GameSettings.cs:83)
GameMaster.LoadCharacter () (at Assets/Game Data/Player Data/Scripts/Character Stats/GameMaster.cs:48)
GameMaster.Start () (at Assets/Game Data/Player Data/Scripts/Character Stats/GameMaster.cs:21)
SCRIPT :
using UnityEngine;
using System.Collections;
using System;
public class BaseCharacter : MonoBehaviour {
private string _name;
private int _level;
private uint _freeExp;
private Attribute[] _primaryAttribute;
private Vitals[] _vitals;
private Skills[] _skills;
public void Awake()
{
_name = string.Empty;
_level = 0;
_freeExp = 0;
_primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
_vitals = new Vitals[Enum.GetValues(typeof(VitalName)).Length];
_skills = new Skills[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 += exp;
CalculateLevel ();
}
public void CalculateLevel()
{
}
private void SetupPrimaryAttributes()
{
for(int cnt = 0; cnt < _primaryAttribute.Length; cnt++)
{
_primaryAttribute[cnt] = new Attribute();
_primaryAttribute[cnt].Name = ((AttributeName)cnt).ToString();
}
}
private void SetupVitals()
{
{
for(int cnt = 0; cnt < _vitals.Length; cnt++)
_vitals[cnt] = new Vitals();
}
SetupVitalModifiers();
}
private void SetupSkills()
{
{
for(int cnt = 0; cnt < _skills.Length; cnt++)
_skills[cnt] = new Skills();
}
SetupSkillModifiers();
}
public Attribute GetPrimaryAttributes(int index)
{
return _primaryAttribute[index];
}
public Vitals GetVitals(int index)
{
return _vitals[index];
}
public Skills GetSkills(int index)
{
return _skills[index];
}
private void SetupVitalModifiers()
{
//health
GetVitals((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Stamina), 10f));
GetVitals((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Endurance), 2f));
GetVitals((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Agility), .5f));
GetVitals((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Dexterity), .25f));
//power
GetVitals((int)VitalName.Power).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Intelligence), 10f));
GetVitals((int)VitalName.Power).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Wisdom), 2f));
GetVitals((int)VitalName.Power).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Charisma), 2f));
}
private void SetupSkillModifiers()
{
//melee
GetSkills((int)SkillName.Melee_Damage).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Strength), 2f));
GetSkills((int)SkillName.Melee_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Endurance), 1f));
GetSkills((int)SkillName.Melee_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Stamina), 1f));
//magic
GetSkills((int)SkillName.Magic_Damage).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Wisdom), 3f));
GetSkills((int)SkillName.Magic_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Intelligence), 1.5f));
//ranged
GetSkills((int)SkillName.Ranged_Damage).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Dexterity), 4f));
GetSkills((int)SkillName.Ranged_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Agility), 2.25f));
//healing
GetSkills((int)SkillName.Healing).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Charisma), 3.5f));
GetSkills((int)SkillName.Healing).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Intelligence),1.5f));
GetSkills((int)SkillName.Healing).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Wisdom), .5f));
}
public void StatUpdate()
{
for(int cnt = 0; cnt < _vitals.Length; cnt++)
_vitals[cnt].Update ();
for(int cnt = 0; cnt < _skills.Length; cnt++)
_skills[cnt].Update ();
}
}
Any ideas? Thanks in advance!
Look at the error!
The IndexOutOfBounds happens at line 85, in the GetVitals method. That again is called from another script, in line 83 of GameSettings in the LoadCharacterData method. This means that LoadCharacterData is calling GetVitals with an argument that's outside the _vitals array range.
Without seeing the GameSettings script, it's kinda hard to say what's happening exactly. As _vitals is set up in the Awake method, and the error is happening during Game$$anonymous$$aster.Start(), so it's not an error with the array not being initalized yet... probably.
Try throwing in this on top of GetVitals:
if(index > _vitals.Length - 1)
Debug.Log("out of bounds with _vitals length: " + _vitals.Length + " and index: " + index);
that'll give you some usefull feedback on what's happening.
Your answer
Follow this Question
Related Questions
Reference error -_- Annoyed please help 1 Answer
I have been watching his tutorial but nothing happens 0 Answers
wierd not existing in current context error caused by 2 lines of code 2 Answers
how do i fix this error 2 Answers
The console no longer takes you directly to the error in the script? 1 Answer