having issue with Index out of range

I’m having issue with my index going out of range when gets too end off the array. How would I reset?

void OnGUI()
{
ChangeCharacterIndex();
GUILayout.BeginArea(new Rect(SelectScreenPos.x,SelectScreenPos.y,SelectScreenPos.width,SelectScreenPos.height));
//GUILayout.RepeatButton
GUILayout.EndArea();
}
void ChangeCharacterIndex()
{
GUI.Button(new Rect(50, 75, 75, 50),Ca.CharNames[Ca.NameIndex]);
if(GUI.Button(new Rect(xPos, 10, 25, 25),“<”))//Here
{
Ca.NameIndex --;

}
if(GUI.Button(new Rect(50, 10, 25, 25),“>”))//Here
{
Ca.NameIndex++;

}
}

}

value = Mathf.Clamp(value, min, max)

Reference:

I will see if that works ty

Perhaps a condition checking whether the ca.nameindex value is out of bounds?

if(ca.nameindex < 0 || ca.nameindex > ca.charnames.length - 1)
{
increment or decrement ca.nameindex respectively
}
else
{
out of bounds logic, maybe start at the highest val if decrementing, or vice versa to create a loop through the names
}

well looking at my info I don’t think I have gave enough info I have for example a string array with 5 names and index starts at 0 when I press the arrow gui buttons it increases to change the name to whats next in my string but when it gets to the last it gives me a error.

Thanks VTV your code worked for one of my buttons.