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 shay4545 · Oct 25, 2014 at 07:39 PM · c#androidarrayindexoutofrangeexception

Index out of Range Exception Error

Can't seem to find the issue.. I have checked all of my gameobjects and my script is only on one of them. The error I'm getting is:

IndexOutOfRangeException: Array index is out of range. SelectCharacter.Update () (at Assets/Scripts/SelectCharacter.cs:17)

This is the main script

 using UnityEngine;
 using System.Collections;
 
 public class SelectCharacter : MonoBehaviour {
 
     public static int chickenSelect;
     public bool rainbowUnlocked;
     public bool halloweenUnlocked;
     public SpriteRenderer Chicken;
     public Sprite[] chickenSprite;
 
     void Start(){
         chickenSelect = 0;
     }
 
     void Update(){
         Chicken.sprite = chickenSprite [chickenSelect];
         if(chickenSelect > chickenSprite.Length){
             chickenSelect = 0;
         }
         if (chickenSelect < chickenSprite.Length) {
             chickenSelect = chickenSprite.Length;        
         }
     }
 }

Then I have some scripts on my arrow buttons:

 using UnityEngine;
 using System.Collections;
 
 public class RightArrow : MonoBehaviour {
 
     void OnMouseDown(){
         SelectCharacter.chickenSelect++;
     }
 }

and left one

 using UnityEngine;
 using System.Collections;
 
 public class LeftArrow : MonoBehaviour {
 
     void OnMouseDown(){
         SelectCharacter.chickenSelect--;
     }
 }

If anyone knows why I am gettting the error it would be very helpful.

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

2 Replies

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

Answer by Unitraxx · Oct 25, 2014 at 07:47 PM

I see 1 mistake that's definitely going to cause an index out of range since you assume that chickenSprite.Length is the last index, while chickenSprite.Length - 1 is the last index.

And then you also want your index adaptation to happen before the actual access which gets you:

 void Update(){
     if(chickenSelect > chickenSprite.Length){
         chickenSelect = 0;
     }
     if (chickenSelect < chickenSprite.Length) {
         chickenSelect = chickenSprite.Length - 1;
     }
     Chicken.sprite = chickenSprite [chickenSelect];
 }
Comment
Add comment · Show 3 · 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 mattyman174 · Oct 25, 2014 at 10:09 PM 0
Share

He needs to also initialize his chickenSprite Array.

avatar image Unitraxx · Oct 25, 2014 at 10:15 PM 0
Share

Hmm I kinda assumed that the initialization was just not included in the sample, but you are definitely right. Another strange thing is that it never gets populated with any values (which would've thrown a more suiting Exception if not initializing is also part of the problem.)

avatar image shay4545 · Oct 25, 2014 at 11:02 PM 0
Share

Thanks for your help guys! Got things figured out and working.

avatar image
0

Answer by Pathojen · Mar 23, 2019 at 12:29 AM

Hello.

I have the same issue in my project, but I haven't even written any scripts. All I did was import a few sprites and animations. (Another issue is that my console will not pop up for me to get a better look at it, which I think is odd.) Any advice on what might be going on? Thanks in advance.

The error did not occur until after I switched the platform to mobile.

Comment
Add comment · 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

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

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

Related Questions

Alter a GameObjects(Clone) array from another script C# 0 Answers

Multiple Tiled GPU warning: RenderTexture color surface errors? 0 Answers

Wierd IndexOutOfRangeException : Array index is out of range 1 Answer

What's a more efficient way to sort an array into multiple groups? 1 Answer

Replace float v=Input.GetAxis("Vertical") with touch? 0 Answers


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