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)
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.