Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Bill Struwe · Aug 03, 2013 at 04:24 PM · arrayindexindexoutofrangeexception

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();
     }
 }
Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linus · Aug 03, 2013 at 04:31 PM 0
Share

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.

avatar image Bill Struwe · Aug 03, 2013 at 04:49 PM 0
Share

How do I do that exactly? Im getting now an Unexpected '('

avatar image Linus · Aug 03, 2013 at 04:59 PM 0
Share
 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.

avatar image Bill Struwe · Aug 03, 2013 at 05:11 PM 0
Share

I updated the question with a new problem

avatar image Linus · Aug 03, 2013 at 05:27 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bill Struwe · Aug 04, 2013 at 04:12 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IndexOutOfRangeException: Array index is out of range. 0 Answers

Has anyone seen this error before: IndexOutOfRangeException: Array index is out of range. UnityEditorInternal.... 2 Answers

What means this error ? 1 Answer

Array of transform index out of range error 1 Answer

playing a different animation in an array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges