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