Here is how to determine which sprite index is missing

I wanted to extend Ben’s example for throwing the error when sprite index was missing. Just saying it’s missing still leaves the questions of which instance is missing the sprite index, and which index is missing?

Yes, we can look at each sprite individually, but by updating the LogError message it can just tell us, so I wrote the method in Brick.cs like this:

	void LoadSprites () {
	int spriteIndex = timesHit - 1;
	if (hitSprites[spriteIndex]) {
		this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
	} else {
		Debug.LogError ("Brick sprite " + this.name + " missing index for " + spriteIndex + ".");
	}
}

The result in the console will be something like:
Brick sprite {instance} missing index for {int}.

Brick sprite Red missing index for 2.

Privacy & Terms