changing GUI Button text with a string array

I am working on a character selection screen.

Thought it would save me some time to store different button states in an array (which could also be sent to a playerStatus script. I plan for the text in the button to change with each click. A material changing script will access this ‘On GUI’ and change the character’s materials appropriately.

I must have the syntax wrong, because Unity is telling me that the ‘System.Type’ does not support slicing’.

I have searched in vain for somebody else trying to do this, can anyone see what I am doing wrong?

Thanks in advance for any help.

var eyesColour = String["Blue","Green","Brown"];
var hairColour = String["Blonde","Brown","Red","Black"];
var dressColour = String["Grey","Brown","Blue","Black"];

GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -250, 250, 50), "Eyes: ");
if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 250, 250, 50), eyesColour[0]))
	{}
GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -200, 250, 50), "Clothing: ");
if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 200, 250, 50), dressColour[0]))
	{}
GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -150, 250, 50), "Hair: ");
if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 150, 250, 50), hairColour[0]))
	{hairColour++;}

The string arrays are badly declared, what’s producing this error. But there are other errors too: you must place the GUI code inside the function OnGUI, and you should have index variables eyes, hair and dress to indicate which are the currently selected options - you can’t increment a string array (like in hairColour++), only its index.

The fixed code is:

var eyesColour: String[] = ["Blue","Green","Brown"];
var hairColour: String[] = ["Blonde","Brown","Red","Black"];
var dressColour: String[] = ["Grey","Brown","Blue","Black"];
var eyes = 0; // currently eyes color selected
var hair = 0; // currently hair color selected
var dress = 0; // currently dress color selected

function OnGUI(){
    GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -250, 250, 50), "Eyes: ");
    if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 250, 250, 50), eyesColour[eyes])){
        eyes = (eyes+1) % eyesColour.length; // rotate eyes inside the options 
    }
    GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -200, 250, 50), "Clothing: ");
    if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 200, 250, 50), dressColour[dress])){
        dress = (dress+1) % dressColour.length;
    }
    GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -150, 250, 50), "Hair: ");
    if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 150, 250, 50), hairColour[hair])){
        hair = (hair+1) % hairColour.length;
    }
}

Read the variables eyes, hair and dress to know the currently selected color for each item.

GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -250, 250, 50), "Eyes: ");
if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 250, 250, 50), eyesColour[0]))
{}
GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -200, 250, 50), "Clothing: ");
if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 200, 250, 50), dressColour[0]))
{}
GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -150, 250, 50), "Hair: ");
if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 150, 250, 50), hairColour[0]))
{hairColour++;}

in the above code you are changing drawing the same text again again, not the arry of strings because the in the text parameter of the button u r using eyesColor[0], hairColor[0] and dressColor[0]

instead of that you have to declare an index controller for each array like this
jus rewrite as below,

var eyeIndex=0;
        var dressIndex=0;
        var hairIndex=0;
            GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -250, 250, 50), "Eyes: ");
            if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 250, 250, 50), eyesColour[eyeIndex]))
                { 
    eyeIndex++;
    }
            GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -200, 250, 50), "Clothing: ");
            if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 200, 250, 50), dressColour[dressIndex]))
                {  
    dressIndex++;
    }
            GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -150, 250, 50), "Hair: ");
            if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 150, 250, 50), hairColour[hairIndex]))
                {hairColour++;
    hairIndex++;
    }

hope it helps :slight_smile: