- Home /
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++;}
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.Fire$$anonymous$$ode[i]); } }
where tacticalvariable.phaser.Fire$$anonymous$$ode[i] is a String variable that is defined in tacticalvariables.js
I can't get anything to show on the button though - why not?
Answer by aldonaletto · Dec 27, 2011 at 02:31 AM
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.
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.
It uses strings from the script for the initial setup. You can also use Reset to change back to the default script values.
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.
Answer by flamy · Dec 27, 2011 at 07:14 AM
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 :)
I guess I never quite understood that the Index keeps the place in the array. Thanks for the tips
Your answer
Follow this Question
Related Questions
Create GUI based on an array 2 Answers
Gathering GameObjects and then creating a button for each GameObject?? 2 Answers
How to output a file to a single string? 1 Answer
how to change a gui button texture 1 Answer
sort gui button 0 Answers