Changing sprites from spritesheet on collision. What am I doing wrong?

My code is listed below in a pastebin link. I’ve tried my best, but I just can’t seem to do it.

I’m trying to get a block to switch to a different sprite every time it is collided with, and finally be destroyed. I just… Can’t seem to get it working when I have switched to a sprite sheet. Help please :slight_smile:

http://pastebin.com/HBST56vZ

I completely re-wrote your script, making it less complicated and fixing your problem:

using UnityEngine;
using System.Collections;

public class block : MonoBehaviour {
	public Sprite[] blockColors; //Assign the length of the array in the inspector,
	//and drag the sprites you want it to change to into each element.
	public int currentSprite = -1; //The int that tells us which sprite in the array
	//we will use.
	public bool deleting; //Unity doesn't actually delete an object until the next
	//frame, so this will allow us to make sure the code doesn't break by having
	//currentSprite be longer than the array length.
	private int arrayLength;

	void Start() {
		arrayLength = blockColors.Length - 1;
	}

	void OnCollisionEnter2D() {
			currentSprite += 1; // adds 1 to the "currentSprite" integer.

		if (currentSprite > arrayLength) { //Deletes the object if currentSprite is greater than the
			//length of the blockColors array
			deleting = true;
			GameObject.Destroy (gameObject);
		}
		if (!deleting) {
			GetComponent<SpriteRenderer> ().sprite = blockColors [currentSprite]; //sets the SpriteRenderer's
			//Sprite to be the desired sprite in the blockColors array.
		}
	}
}