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 15, 2014 at 10:48 PM · arrayindex

IndexOutOfRangeException error

Im getting this error: IndexOutOfRangeException: Array index is out of range. ShowGameover.Start () (at Assets/Scripts/ShowGameover.cs:14)

This is the script:

 using UnityEngine;
 using System.Collections;
 
 public class ShowGameover : MonoBehaviour {
 
     private float posY;
     public float endPosY;
     public SpriteRenderer gameoverBox;
     public Sprite[] gameoverSprites;
 
     // Use this for initialization
     void Start () {
         posY = transform.position.y;
         gameoverBox.sprite = gameoverSprites [0];
     }
     
     // Update is called once per frame
     void Update () {
         transform.position = new Vector3(transform.position.x, posY, transform.position.z);
         if (Movement.dead == true) {
             posY += Time.deltaTime * 10f;    
             if(posY >= endPosY){
                 posY = endPosY;
             }
         }
         if (Movement.rightPoints >= 10 || Movement.upPoints >= 10 || Movement.downPoints >= 10) {
             gameoverBox.sprite = gameoverSprites[1];        
         }
         if (Movement.rightPoints >= 20 || Movement.upPoints >= 20 || Movement.downPoints >= 20) {
             gameoverBox.sprite = gameoverSprites[2];        
         }
         if (Movement.rightPoints >= 50 || Movement.upPoints >= 50 || Movement.downPoints >= 50) {
             gameoverBox.sprite = gameoverSprites[3];        
         }
         if (Movement.rightPoints >= 100 || Movement.upPoints >= 100 || Movement.downPoints >= 100) {
             gameoverBox.sprite = gameoverSprites[4];        
         }
     }
 }

I don't know why the error is showing up, but it doesnt seem to affect anything but it happens whenever I press start and then again each time when Movement.rightpoints is greater than one of the values above. I have 5 different sprites assigned to the array in the hierarchy menu. I also have the sprite renderer checked in the hierarchy.

Comment
Add comment · Show 2
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 robertbu · Oct 15, 2014 at 11:23 PM 0
Share

You are going to get this error if the 'gamoverSprites' array has size 0 in Inspector. If you find it initialized, look for a second copy of this script on another game object.

avatar image shay4545 · Oct 15, 2014 at 11:43 PM 0
Share

Ah! $$anonymous$$y bad I still had the script on two other gameobjects before I made changes to it. Thanks @robertbu

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Habitablaba · Oct 15, 2014 at 11:25 PM

Since you aren't initializing gameoverSprites to anything, you get that error in the start method. It doesn't seem like a problem, since you are assigning values in the inspector, but I wonder if you aren't doing that until some time after Start has been called?

What do you mean by 'greater than one of the values above'? On what line do you get the error in that case?

Also note that if Movement.rightPoints >= 100, you will assign gameoverBox.sprite a total of 4 times, since each if statement will be true (since 100 is greater than 10, 20, and 50 as well). In order to stop that, consider using 'else if' instead of just 'if' for the last three checks.

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 shay4545 · Oct 15, 2014 at 11:36 PM 0
Share

how do I initialize gameoverSprites to something? if $$anonymous$$ovement.rightPoints is >=10 then the error is on line 27. for >= 20 it is line 30.

avatar image KpjComp · Oct 15, 2014 at 11:41 PM 0
Share

That sounds like you have only assigned 1 sprite to the gameoverSprites. Look at your gameoverSprites property in the editor, you should have Size = 5, Element 0, Element 1, Element 2, Element 3, Element 4

avatar image shay4545 · Oct 15, 2014 at 11:44 PM 0
Share

Never$$anonymous$$d I found the issue. I had the script on other gameobjects as well. Thanks for the help though! @Habitablaba

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Trouble with targeting enemies from a List 1 Answer

implementing shuffle script 0 Answers

IndexOutOfRangeExeption - Array index is out of range 2 Answers

array knowledge is cloudy at best 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