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 CuddleMonster · Oct 26, 2013 at 12:36 AM · arrayrangeindex

Array Index out of range?

enter code hereI'm quite confused by this little issue. I've dealt with arrays and classes before, but clearly I've made some sort of mistake. I keep getting Array Index is out of range on the line:

Equip(AllSkills[0],0);

AllSkills, the Array of the class "Skill" has members in it... as does SkillSlot(Though I believe the out of range is refering only to AllSkills). I added these members in the inspector and they are filled out.

What is my mistake?

class Skill {

 //Active or Passive
 var Active : boolean;
 
 //SkillObject
 var TheObject: GameObject;

 var CooldownCurrent : float;
 var CurrentStamina : int;
 var StaminaCost : int;
 
 }
 
 var AllSkills : Skill[];
 
 
 var SkillSlot : Skill[];
 
 
 function Equip ( NewSkill : Skill, Slot : int ) {
 
     SkillSlot[Slot] = NewSkill;
 
     if (!SkillSlot[Slot].Active) {
         Instantiate (SkillSlot[Slot].TheObject,transform.position,transform.rotation);
     }
 
 
 
 }
 
 function Activate ( Slot : int ) {
 
 
     if (SkillSlot[Slot].CooldownCurrent == 0 && SkillSlot[Slot].StaminaCost < CurrentStamina ) {
         if (SkillSlot[Slot].Active) {
             Instantiate (SkillSlot[Slot].TheObject,transform.position,transform.rotation);
         }
 
     }
 
 }
 
 
 function Update () {
 
     if( Input.GetKeyDown(KeyCode.Alpha1)) {
         Activate(1);
     }
     if( Input.GetKeyDown(KeyCode.Alpha2)) {
         Activate(2);
     }
 
     
 if(Input.GetKeyDown(KeyCode.T)) {
         
        Equip(AllSkills[0],0);
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Oct 26, 2013 at 12:44 AM

AllSkills is never initialized in this code. It is a public variable, so the Inspector will create it for you at a size of 0. With a size of 0, any access you attempt will generate an index out of range. To fix it, you can either go the inspector and set the size of the array, or you can allocate space for teh array in code. If you do the latter, then you probably should make the array 'private'.

Comment
Add comment · Show 8 · 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 CuddleMonster · Oct 26, 2013 at 12:45 AM 0
Share

It is filled in in the inspector

avatar image robertbu · Oct 26, 2013 at 12:48 AM 0
Share

If it is filled in the inspector, the next check should be to see how many game object have this script attached. You can use the search field at the top of the Hierarchy view. This problem will occur if you have it attached to more than one game object and have only initialized one of those objects.

avatar image robertbu · Oct 26, 2013 at 01:00 AM 0
Share

I'm on shaky ground here but it doesn't appear that your skills class is serializable. I don't know how Unity handles the size of an array pointing to non-searializable objects.

http://docs.unity3d.com/Documentation/ScriptReference/Serializable.html

avatar image CuddleMonster · Oct 26, 2013 at 01:13 AM 0
Share

This is the only object with it attached, but it would not matter if it wasn't. It shouldn't need to be serialized, based on my memory of using classes in the past, however Its worth looking into, I suppose, since I haven't touched them in a few months.

avatar image CuddleMonster · Oct 26, 2013 at 01:18 AM 0
Share

No; Serializing it would not effect it in the slightest.

Show more comments

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

17 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

Related Questions

Arrays rebels to my power! 1 Answer

Array Index Out of Range 2 Answers

OnGui function - Array is out of index question 1 Answer

Index out of range exception - C# - Can't find the problem 1 Answer

Array index is out of Range!? 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