- Home /
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.
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.
Ah! $$anonymous$$y bad I still had the script on two other gameobjects before I made changes to it. Thanks @robertbu
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.
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.
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
Never$$anonymous$$d I found the issue. I had the script on other gameobjects as well. Thanks for the help though! @Habitablaba
Your answer
![](https://koobas.hobune.stream/wayback/20220613165208im_/https://answers.unity.com/themes/thub/images/avi.jpg)