Array Out of Range [DIFFERENT THAN PREVIOUS POST]

Hello all,

When I am going to swap out my sprites to show they have been damaged, on collision I get an error in the console that says:

IndexOutOfRangeException: Array index is out of range.
brick.LoadSprite () (at Assets/scripts/brick.cs:35)

I know that this is the same error that a previous user had, but I do not have the same error that they do. Here is the code for my brick prefab:

using UnityEngine;
using System.Collections;

public class brick : MonoBehaviour {

public int maxHits;
private int timesHit;
public Sprite[] hitSprites; 
public static int brickCount = 0;

// Use this for initialization
void Start () {
timesHit = 0;
}

// Update is called once per frame
void Update () {

}

void OnCollisionEnter2D (Collision2D collision) {
    timesHit++;
    if (timesHit == maxHits) {
        Destroy(gameObject);
    } else {
        LoadSprite();
    }
}
void LoadSprite() {
    int spriteArrayPos = timesHit - 1;
    //BELOW IS LINE 35
    this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteArrayPos]; 
}

}

And then here is the inspector for my 2 hit and 3 hit brick prefabs

Is there something that I am doing amazingly wrong? If so, let me know because I am stumped!

Best,
Tobin

Try putting a Debug.Log ("times hit " + timesHit); in your loadsprite just to see what timesHit value is.

It might be that its registering multiple hits.

If it is then you might have to check if its >= max hits in the IF statement. Just a thought

2 Likes

Try these things:

  1. Change the IF condition to this:
  1. Check that the Brick.CS script is attached as a component only to bricks objects, by typing “brick” in the search field of the Hierarchy window

  2. Check that the timesHit++ line is present only in the brick.cs script, by using CTRL+ALT+F in Mono searching in the Whole solution.

1 Like

has anyone solved this? i seem to be having the exact problem.

2 and 3 hit bricks break after reaching max hits but the sprites did not change.

i get:
IndexOutOfRangeException: Array index is out of range.
Brick.LoadSprites () (at Assets/scripts/Brick.cs:35)
Brick.OnCollisionEnter2D (UnityEngine.Collision2D col) (at Assets/scripts/Brick.cs:29)

Hello @jason_mcpherson

What is the size of the array? What is the index value that is being used?

It looks like maxHits is 3, and you test if timesHit == maxHits. (capitalization changed when I realized not capitalizing made a swearword). You also have two bricks in the array, so the array indices allowed are 0 and 1. So, I’ll step through the code like they showed us back in CS211 (a Pascal–remember that?–based computer science intro) class decades ago:

So, timesHit==0 initially.

First time OnCollisionEnter2D called: timesHit ==1, load sprite at index 0
Second time: timesHit == 2, load sprite at index 1
Third time, destroy game object.
fourth time (this can happen! game object can take time to destroy): timesHit = 4, load sprite at index 3, out of bounds.

So, change if (timesHit == maxHits) { to if (timesHit >= maxHits) { and see if that works.

Hi,
I had the exact same problem and I found that all my blocks (although they say they are prefabs) in the game didn’t update after I added all the other sprites. I suggest to test this out, add a new prefab and play… This worked for me! Though frustrating as I may have to set my level up again.

1 Like

This is the correct answer. Thanks!

Privacy & Terms