Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by mcmikecreations · Aug 21, 2016 at 10:22 PM · c#scripting problemeditorarrayout of range

Array index out of range. But it doesn't appear to be.

So, I have these variables:

 public Sprite[] items;
     public Image[] slots;

In editor, I filled 63 items with sprites and 36 slots with UI images /w text & image as children from scene. Then I have this function:

 void LoadItems(int count)
     {
         for (int i = 0; i < count; i++)
         {
                 
                 if (character.items[i].count > 0)
                 {
                 //Debug.Log("a");
                 slots[i].color = new Color(1, 1, 1, 1);
                 slots[i].sprite = items[character.items[i].id];
                 slots[i].transform.GetChild(0).GetComponent<Text>().text = character.items[i].count.ToString();
             }
             else
             {
                 slots[i].sprite = null;
                 slots[i].transform.GetChild(0).GetComponent<Text>().text = null;
                 slots[i].color = new Color(1, 1, 1, 0);
             }
         }
     }

Which is called from 2 places which call errors:

 if (Time.frameCount == 1 && character != null)
             {
                 LoadItems(slots.Length);
             }
     //---------------
     if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
                 {
                     Debug.Log("ai");
                     LoadItems(9);
                 }
 

Error: IndexOutOfRangeException: Array index is out of range.

I really don't know, what is wrong. Maybe it's the editor? Oh and character.items is this:

 public Item[] items;


 //This is Image script
     [System.Serializable]
     public class Item {
     
         public int id;
         public byte count;
         public string info;
     
         public Item(int id, byte count, string info)
         {
             this.id = id;
             this.count = count;
             this.info = info;
          }
     }

Edit: items[] is just an array of sprites, used to represent textures. I have 63 in-game textures, so I have 63 Sprites, 1 for each item. Slots on the other hand are actual on-screen inventory slots, where the items are stored. And character.items is basically the same array as slots, it holds data of each slot, what the item is (id, also used in Sprite[] items), what is it's count, and additional info.

Comment
Add comment · Show 5
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 ScaniX · Aug 21, 2016 at 10:25 PM 0
Share

"I filled 63 items with sprites and 36 slots"

I guess that is a typo and both arrays have the same length? Please also tell us what line is causing the error. I suspect this one:

 items[character.items[i].id];

Did you check the value of "id"?

Edit: Just saw you have three arrays, so: Did you check that character.items.Length >= slots.Length and all ids from the items are < items.Length?

avatar image mcmikecreations ScaniX · Aug 22, 2016 at 02:31 PM 0
Share

character.items and slots have the save length, ids from character.items range from 1 to 18 and items.length is 63

avatar image mcmikecreations ScaniX · Aug 22, 2016 at 08:56 PM 0
Share

yes, that is the line with the error

avatar image ScaniX mcmikecreations · Aug 22, 2016 at 09:49 PM 0
Share

Well, if that is the line, you can simply printout the values to see exactly what is wrong there.

Put this line right before the line with the error:

 Debug.Log("slots.Length: " + slots.Length + "\nitems.Length: " + items.Length + "\ncharacter.items.Length: " + character.items.Length + "\ni: " + i + "\ncharacter.items[i].id: " + (character.items.Length > i ? character.items[i].id : "undefinded"));
avatar image mcmikecreations · Aug 22, 2016 at 02:29 PM 0
Share

Also, I've set the array length and their content in the editor, so @$$anonymous$$oSR your " = new ..." lines will erase the sprites and images, that I assigned.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ArturoSR · Aug 21, 2016 at 10:40 PM

Hi, ok, I check your code and seems everything it is ok, but maybe you forgot a little detail:

 public Sprite[] items = new Sprite[0];
 public Image[] slots = new Image[0];
 
 //In some part of the code
 items = new Sprites[5]; // this creates a new array with five elements.
 images = new Image[10]; //Same as before.
 
 if (items.Length < images.Length) { //Of course it is. ;-)
     //Do Something here. . .
 }

If you don't do this, you will get the Debug Error: out of range, count 1 - existing 0.

Comment
Add comment · Show 2 · 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 mcmikecreations · Aug 22, 2016 at 02:32 PM 0
Share

This will erase editor's assignments, maybe I should load stuff from the script, not using the editor?

avatar image mcmikecreations · Aug 22, 2016 at 09:10 PM 0
Share

I did try this, the errors went away, but now everything I've set up in the inspector is gone.

avatar image
0

Answer by mcmikecreations · Aug 22, 2016 at 10:19 PM

Okay, thanks everyone, who answered, it DID help me. The problem was: IDK why, but unity keeps screwing up it's editor assignments. So, I decided to assign every sprite and image in script, using @ArturoSR answer. Thanks to you all.

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 ArturoSR · Aug 23, 2016 at 07:55 AM 0
Share

Hey thanks for your comments, what I showed before was only an example that how is suppose to manage an array of objects, no matter the type, sorry if a make you troubled, but I don't have to much free time yesterday to write new comments, but a see that you could manage the problem very well, maybe it is a little to late but, how suppose you got the items and images in first place?, because, if you let me say this, I works a lot with this type of cases and I never get my component screwed up by the editor, I recommend to you to think about the inspector more like a block of pieces, one at a time, this way could be more easy to you, any doubts you can contact me any time, cheers.

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

6 People are following this question.

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

Related Questions

Multiple Tags with FindGameObjectsWithTag...? 1 Answer

Soundarray for player is not working properly 1 Answer

Custom inspector. Pick one bool from a list. 1 Answer

PostProcessingFactory.cs 2 Answers

How do i get List of all Planes GameObjects ? 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