- Home /
Array index is out of range error Edited
I am following Burgzerg Arcade's tutorials ( I know its late ), and I am stuck pretty early into it, during the Character Creation vids. My error is in the BaseCharacter script, but the strange thing is, its only in vitals, not attribs or skills:
BaseCharacter.GetVital (Int32 index)
If it is as simple as a typo, I am not seeing it. I have followed his tutes and the general scripting rules to the letter.
RE-EDIT: Back to original problem. Posted my code again. I'm sure I changed something else, but it should still work for you (but not me, obv)
Script:
using UnityEngine;
using System.Collections;
using System; //access enum class
public class BaseCharacter : MonoBehaviour {
private string _name;
private int _level;
private uint _freeExp; //store more exp available
private Attribute[] _primaryAttribute;
private Vital[] _vital;
private Skill[] _skill;
public void Awake() {
_name = string.Empty;
_level = 0;
_freeExp = 0;
//how big it will be
_primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
_vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
_skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
SetupPrimaryAttribute();
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();
}
//take avg of all of the skills and assign that as the level
public void CalculateLevel() {
}
//set up the attrib vit skills
private void SetupPrimaryAttribute() {
//iterate thru array
for(int count = 0; count < _primaryAttribute.Length; count++) {
_primaryAttribute[count] = new Attribute();
_primaryAttribute[count].Name = ((AttributeName)count).ToString();
}
}
private void SetupVitals() {
for(int count = 0; count < _vital.Length; count++) {
_vital[count] = new Vital();
}
SetupVitalModifiers();
}
private void SetupSkills() {
for(int count = 0; count < _skill.Length; count++) {
_skill[count] = new Skill();
}
SetupSkillModifiers();
}
public Attribute GetPrimaryAttribute(int index) {
return _primaryAttribute[index];
}
//Debug.Log("index is: " + index.ToString() + " length is:" + _vital.Length.ToString());
public Vital GetVital(int index) {
return _vital[index];
}
public Skill GetSkill(int index) {
return _skill[index];
}
private void SetupVitalModifiers() {
//health
GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 0.5f));
//energy
GetVital((int)VitalName.Energy).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 1));
//mana
GetVital((int)VitalName.Mana).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), 1));
}
private void SetupSkillModifiers() {
GetSkill((int)SkillName.Melee_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Strength), .33f));
GetSkill((int)SkillName.Melee_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Agility), .33f));
GetSkill((int)SkillName.Melee_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
GetSkill((int)SkillName.Melee_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), .33f));
GetSkill((int)SkillName.Magic_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Concentration), .33f));
GetSkill((int)SkillName.Magic_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), .33f));
GetSkill((int)SkillName.Magic_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Concentration), .33f));
GetSkill((int)SkillName.Magic_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), .33f));
GetSkill((int)SkillName.Ranged_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Concentration), .33f));
GetSkill((int)SkillName.Ranged_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
GetSkill((int)SkillName.Ranged_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
GetSkill((int)SkillName.Ranged_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Agility), .33f));
}
public void StatUpdate() {
//iterate thru vitals skills, etc
for(int count = 0; count < _vital.Length; count++)
_vital[count].Update();
for(int count = 0; count < _skill.Length; count++)
_skill[count].Update();
}
}
On the line before the error, do a Debug.Log() to see what the index is. And perhaps also the length of the array to see if it is the length it should be.
How do I do that exactly? Im getting now an Unexpected '('
Debug.Log("index is: "+index+" length is:"+_vital.Length);
Also, you can copy the full error message by selecting it in the log, and hit CTRL+C (if on windows), please post it as part of the question.
EDIT: Was the original question answered?, if so a new question should be made.
Was that after adding in the Debug.Log, C# might need that line to be:
Debug.Log("index is: "+index.ToString()+" length is:"+_vital.Length.ToString());
Or remove the line.
Answer by sparkzbarca · Aug 04, 2013 at 03:34 AM
attribute is a class not an enum.
Basically the whole issue is that your telling it
Attribute[Enum.GetValues(typeof(Attribute)).Length];
what this means is hey you know that enumerator i set up called attribute.
I want you to get me each value of those.
The compiler is saying wait a minute, the enumerator attribute doesnt exist.
Which we know it doesnt because Attribute is class.
You need to figure out the name of the enum
Perhaps it's stored in the attribute class and he does something like
Attribute.AttributeEnum
I dont know.
It was the Enumeration name... Attribute[Enum.GetValues(typeof(AttributeName)).Length];
After following his tutes some more, I have the old error again!
I am trying the codes Linus gave me, but I am getting the Unexpected ( again for each one I try.
Could it be that I'm using a newer version than Burgzerg did?
Your answer
Follow this Question
Related Questions
IndexOutOfRangeException: Array index is out of range. 0 Answers
What means this error ? 1 Answer