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 /
avatar image
0
Question by chrismcrae5712 · Sep 25, 2014 at 02:55 PM · error

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!

Comment
Add comment · Show 1
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 Baste · Sep 25, 2014 at 03:28 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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