Array won't go to Element 0.

Basically, I have 2 buttons, one called “next” that goes up the array and one called “previous” that goes that the array. When I try to get to zero with the “previous” button I get this error “IndexOutOfRangeException: Array index is out of range.”.

Thanks for helping!
Here’s the function for the button:

public GameObject[] hair;        //Array
private int currentHair = 0;     //The hair that should be displayed
public GameObject hairLocation;  // Where the hair should be instantiated
private int hairLenght;          // The lenght of the array set with void Start()

public void ChangeHairPrev() 
{
	// Sets what hair it should be when clicked
	if(currentHair != 0)
		currentHair--;
	
	if(currentHair == 0)
		currentHair = hairLenght;

//Instantiates the Object
	Instantiate(hair[currentHair], hairLocation.transform.position, hairLocation.transform.rotation);
}

So let’s imagine what’s happening when your current hair is equal to 1.

First is being executed this part:

 if(currentHair != 0)
         currentHair--;

Now you currentHair is equal to 0.
Because you set currentHair to zero, immediately after this lines are being executed:

  if(currentHair == 0)
         currentHair = hairLenght;

And as long as hairLength contains the length of array (indexes of array are from 0 to length-1, btw), you are trying to access out of array’s range.

So here you have two problems: you never access 0 element and you use array’s length as index.