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 stefan1294 · Feb 13, 2014 at 09:26 PM · c#arrayindexoutof

C# - IndexOutOfRangeException - Array index is out of range

I'm attempting to create some sort of GIF animation script. I've sliced an image of 30 frames into 30 different images via the Sprite Editor. Now I'm attempting to loop through them all and changing the background of the GameObject, but it's giving me an error: Array index is out of range.

This error usually means I'm trying to loop through more sprites than the given size of the array. But I'm not sure if that is happening right here. So, this is my full script:

 using UnityEngine;
 using System.Collections;
 
 public class AnimatedPauseMenu : MonoBehaviour 
 {
     int nFrame;
     public Sprite[] pauseSprites;
 
     void Start () 
     {
         nFrame = 0;
         pauseSprites = Resources.LoadAll<Sprite>("pausemenu2");
     }
     
 
     void Update () 
     {
         if (CollisionScript.pPaused == 1 && CollisionScript.pGameOver == 0)
         {
             nFrame++;
             gameObject.GetComponent<SpriteRenderer>().sprite = pauseSprites[nFrame];
             if (nFrame == 30) nFrame = 0;
         }
     }
 }

Error line:

 gameObject.GetComponent<SpriteRenderer>().sprite = pauseSprites[nFrame];

I'm not entirely sure what is causing this problem. However, I did notice one thing. When I'm attempting to change the array value in the Inspector, it automatically resets to 0 right after I changed the size. So I assume the array has a size of 0, but I'm not able to solve it even when I'm using the following line to define the array:

 public Sprite[] pauseSprites = new Sprite[30];

p/s my file names are: pausemenu2_0 to pausemenu2_29

Thanks for helping!

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 SungMin0315 · Mar 19, 2015 at 12:20 PM 0
Share

Hi... i have same problem in my script... 8ㅁ8... if you solve this problem. can you help me?... this is my e-mail if you send email to me, i will send my script please..please help me..ㅠ.ㅠ

e-mail Adress : 1505104@naver.com

2 Replies

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

Answer by gjf · Feb 14, 2014 at 02:11 PM

put the "if (nFrame == 30) nFrame = 0;" line BEFORE setting sprite. the index is out of range before you reset it...

also, doing GetComponent in Update() is expensive. try the following:

 ...
 public Sprite[] pauseSprites;
 private SpriteRenderer _spriteRenderer;
 ...

then in Start/Awake

 _spriteRenderer = gameObject.GetComponent<SpriteRenderer>();

and to use it:

 nFrame++;
 if (nFrame > pauseSprites.Length)
 {
    nFrame = 0;
 }
 _spriteRenderer.sprite = pauseSprites[nFrame];

...

it wouldn't be the worst idea to check that _spriteRenderer is non-null too... little bit of defensive programming rarely hurts ;)

g.

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 stefan1294 · Feb 17, 2014 at 09:34 AM 0
Share

Hey @gjf,

Thanks. But when I store the information inside pauseSprites, it still gives me the array index out of range error. In your example you're not storing the images inside the paueSprites array, so I added it, yet it gives me the same error.

 void Start () 
     {
         pauseSprites = Resources.LoadAll<Sprite>("pausemenu2"); // <- Error line
         _spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
     }
avatar image gjf · Feb 17, 2014 at 11:35 AM 0
Share

that's strange - i did a test with the following pieces of code and it works for me:

 public Sprite[] pauseSprites;

in Awake()

 pauseSprites = Resources.LoadAll<Sprite>("TestSprites");

 Debug.Log(string.Format("pauseSprites.Length={0}", pauseSprites.Length));

prints 10 (as expected)

in your Resources folder, how many parts are in the sprite? is the filename exact (case sensitive/spaces/etc.)?

avatar image
0
Wiki

Answer by Rahazan · Feb 13, 2014 at 10:57 PM

You are resetting nFrame when it's 30, your array is 30 long, so the last index is 29 (0 to 29 are the possible indices).

So you will probably want

 if (nFrame == 29) nFrame = 0;

You could also check against the length of the array (and substract one from it), so that at a later point you want to re-use this script for another "gif" or want to remove a frame you won't have to change the script.

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 stefan1294 · Feb 14, 2014 at 08:36 AM 0
Share

Hey,

Thanks but unfortunately this didn't solve my issue. I'm checking the length of the array right now (pauseSprites.Lenght), but unfortunately it returns 0 no matter what..

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

21 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

Related Questions

Array index is out of Range!? 1 Answer

Wierd Animation Bug 0 Answers

Array out of its own range? 4 Answers

C# array not behaving as expected 0 Answers

helpme RPC 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