Sprites from array won't render

Hello,

I’m trying to randomly render sprites from an array, but for some reason, it’s not doing what I want it to do. Here’s what I have so far:

int randomPawNum;
public Sprite[] catPaw; 

	
void OnMouseDown () {
		randomPawNum = Random.Range (0, 6); 
		GetComponent<SpriteRenderer>().sprite = catPaw[randomPawNum];
		Debug.Log (catPaw[randomPawNum]);
	}

In addition, I get an error that says my index is out of range, but I’m certain the size of my array is 6. Here’s a screenshot for verification:
76200-screen-shot-2016-08-16-at-90908-pm.png

Thanks for reading and any help is appreciated!

The problem is probably your range. When dealing with Random.Range the max is inclusive, so having 0, 6 as your range will give you seven slots to fill. You can fix this by changing your range to (0, 5)
Hope this helped you!

I solved my problem.

Up until now, I’ve been using the void OnMouseDown() method.
I decided to use the GetMouseButtonDown() method inside void Update() and it worked like a charm. Again, I appreciate all the help I got. Hopefully this helps some of you out there.

Here’s what I have right now for reference:

if (Input.GetMouseButtonDown (0)) {
		randomNum = Random.Range (0, 6);
		GetComponent<SpriteRenderer> ().sprite = paws [randomNum];
}