IndexOutOfRangeException: Array index is out of range.

I have a script that has an int array but in unity I get the error “index out of range” and don’t know whats wrong with it
here’s my script

using System.Collections;
using UnityEngine.UI;
using UnityEngine;

public class Inventory : MonoBehaviour {

	public Text inventoryText;

	int[] counts = new int[8];

	void Update () {
		inventoryText.text = "Grass:" + counts[0] + "

Dirt:" + counts[1] + "
Stone:" + counts[2] + "
Drysand:" + counts[3] + "
WetSand:" + counts[4] + "
Coal:" + counts[5] + "
Iron:" + counts[6] + "
Gold:" + counts[7] + "
Diamonds:" + counts[8];

	}

	public void Add (int tileType, int count){
		
	}
}

the arrays the right size( 8 in length)

Try deleting the “counts[8]”. I think you get the error because when you give the array the length of 8 the array has literally 8 spaces, but when you access the variables inside the array the 8 is out of range because the array starts from 0.

Your array initialized for 8 integers (0 to 7) but in your text box you are requesting 9 integers (0-8)
Your solution would be to change int counts = new int[8]; to int counts = new int[9];