Conditional operator problem S:

hi, i want to make a picture selector, and i try to use this line of code:

int avatarSelected = 0;
public Texture[] avatarTextures;

GUI.DrawTexture(new Rect(195.0f, 130.0f, 300.0f, 300.0f), avatarTextures[avatarSelected], ScaleMode.ScaleToFit, alphaBlendEnabled);

        if (GUI.Button(new Rect(525.0f, 250.0f, 45.0f, 50.0f), ">"))
        {
            avatarSelected = (avatarSelected > avatarTextures.Lenght - 1) ? avatarSelected + 1 : avatarSelected = avatarTextures.Lenght - 1;
        }

        if (GUI.Button(new Rect(148.0f, 250.0f, 45.0f, 50.0f), "<"))
        {
            avatarSelected = (avatarSelected < 0) ? avatarSelected - 1 : avatarSelected = 0;
        }

But when i switch the picture from button “<” it works, but “>” continue to go out of array. It’s a bug or is just my fault? S:

I think your tests are wrong.

avatarSelected = (avatarSelected > avatarTextures.length - 1) ? avatarSelected + 1 : avatarSelected = avatarTextures.length - 1;

means that if avatarSelected is more than avatarTextures.length - 1, you increment it. So there is an out of array error.
Your code should be :

int avatarSelected = 0;
public Texture[] avatarTextures;
 
GUI.DrawTexture(new Rect(195.0f, 130.0f, 300.0f, 300.0f), avatarTextures[avatarSelected], ScaleMode.ScaleToFit, alphaBlendEnabled);
 
if (GUI.Button(new Rect(525.0f, 250.0f, 45.0f, 50.0f), ">"))
{
     avatarSelected = (avatarSelected > avatarTextures.length ) ? avatarSelected = avatarTextures.length - 1 : avatarSelected + 1;
}
 
if (GUI.Button(new Rect(148.0f, 250.0f, 45.0f, 50.0f), "<"))
{
     avatarSelected = (avatarSelected < 0) ? avatarSelected = 0 : avatarSelected - 1;
}