x


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++;}
more ▼

asked Dec 27 '11 at 01:58 AM

blutwurstchen gravatar image

blutwurstchen
16 3 3 3

I'm sorry to Necro this, but it seemed to make sense to add to this topic, and keep all the information in one place.

I have the same problem as the OP, that is that I want to define the text if a GUI.Button using a string array, but my string is defined in a different script.

For example:

//to pick up variables defined in other script called tacticalvariables.js

var tacticalvariable : tacticalvariables;
tacticalvariable = gameObject.GetComponent(tacticalvariables);

OnGUI () {
for (var i:int = 0; i<=5; i++) {
   GUI.Button (Rect (270, 10 + (60 * i), 120, 40), tacticalvariable.phaser.FireMode[i]);
   }
}

where tacticalvariable.phaser.FireMode[i] is a String variable that is defined in tacticalvariables.js

I can't get anything to show on the button though - why not?

Jan 15 '12 at 05:59 PM Xipheas
(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest
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 :)

more ▼

answered Dec 27 '11 at 07:14 AM

flamy gravatar image

flamy
3.5k 5 11 37

I guess I never quite understood that the Index keeps the place in the array. Thanks for the tips

Dec 27 '11 at 03:55 PM blutwurstchen
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 27 '11 at 02:31 AM

aldonaletto gravatar image

aldonaletto
41.3k 16 42 195

Thanks for the help.

The GUI still does not use the strings from the script, they need to be added in the inspector, but then it works fine.

Dec 27 '11 at 04:12 AM blutwurstchen

It uses strings from the script for the initial setup. You can also use Reset to change back to the default script values.

Dec 27 '11 at 07:44 AM Eric5h5

It's like @Eric5h5 said: the Editor has an elephant memory, and remember the old values to death - even if you change them in the script. Click the small "gear" button at the right of the script name and select option "Reset" to update the Inspector variables.

Dec 27 '11 at 11:54 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3673
x1356
x1040
x786
x418

asked: Dec 27 '11 at 01:58 AM

Seen: 1302 times

Last Updated: Jan 15 '12 at 09:29 PM