- Home /
Have a GUI.Button string include a variable as part of the string?
So I want to pass a variable into a GUI buttons name. I can easily have the buttons name be the variables value by using:
if (GUI.Button (Rect (10,20,180,80),GameMaster.searchShips.ToString())) {
But how can I include the name and the variable on the end of the string?
So I want the text to read "ships launched" as the string, but then include the directed variable in it, so "ships launched - 10" or whatever the variable is currently at. I cant seem to separate the string from the variable location, so the text just ends up being what I have written in the code, or ignored with errors.
What is the best way to go about this? Any help would be fantastic. Thanks for reading.
If I understand correctly you want the name of the variable and the variable to display?
if(GUI.Button (Rect(10,20,100,80),"Variable Name" + variable))
I tried that before posting, and all though it does compile, the actual text in the box is not correct, I can't see the entire message, but the word compiler appears among others in the box ins$$anonymous$$d of the string and variable.
Could this be because either:
1) The variable it is calling is not contained in the script, and is a public static elsewhere?
2) the GUI is wrapped in an IF statement?
Looks like this when done like that:
function DoWindow0 (windowID : int) {
if (GUI.Button (Rect (10,20,180,80), "Drones Searching : "+ Game$$anonymous$$aster.searchShips.ToString)) {
audio.PlayOneShot(sound);
}
I've played around with it a bit and it works now. Thanks anyway. Not even sure what the problem was, but the way you posted is the correct way to do what I asked, so I'll mark it as the answer.
Well, in your comment above you wrote
Game$$anonymous$$aster.searchShips.ToString
Without the calling brackets. So you would assign the function ToString itself and don't execute it to get the result. Is that a typo here or does your script really looks like this?
You know when you have been staring at something for to long? That happened :D
Your answer
Follow this Question
Related Questions
String to Var 2 Answers
Inputfield set as Variable 1 Answer
Optimizing OnGUI 1 Answer
How can I Include string variables in Rich Text? 1 Answer
Display additional text before the variable that the user is editing in a GUI Text Field 1 Answer