Odd non-bug when checking for OutOfBounds

Following along with the lecture to check for a null hitSprite, I’m encountering a strange issue that I’m confused by.

After removing all the damage sprites from my block instance, when playing the game I will get the Debug.LogError() message, but the sprite of the block changes. I’m not sure why the sprite changes since the array is empty…

Here is my code:

    private void ShowNextHitSprite() {
        int spriteIndex = timesHit - 1;
        if (hitSprites[spriteIndex] != null) {
            GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
        } else {
            Debug.LogError("Block sprite is missing from array [" + gameObject.name + "].");
        }
    }

And a screenshot of my Unity:

Any ideas what might be causing the error and non-existent sprite to be shown?

I found the issue and thought of deleting my question, but I’ll instead post what I found in case other students run into the same thing.

Be careful with Ctrl+D to duplicate your GameObjects. I had apparently gotten a little overzealous when duplicating the blocks and actually had another 2 Hit Breakable Block right on top of the one I deliberately broke for testing.

So I was getting the error because block #7 was indeed missing the sprites. But block #6 was directly under #7 and it’s sprites were working just fine. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms