- Home /
Multiple Buttons To Use One Text Input Field
Is it possible for me to use multiple buttons to use one text input field to name each button? Something like say I have a grid of buttons and I click one button that will set a public bool to true or false, first click = true 2nd click = false but if it's true that I can access the text input field to name that button save that name to player prefs and then turn on my 2nd button and name it and so on?
I have a text input field set up next to my grid of buttons but not sure how to go about setting up the text input field to save each button name? I have set up a script with the following bools and button calls.
//Space Matt Button Bools
public bool sMatt01 = false;
public bool sMatt02 = false;
public bool sMatt03 = false;
//Button State Settings
private int sM01;
private int sM02;
void Awake (){
//Space Matt Button 01
sM01 = PlayerPrefs.GetInt ("spaceMatt01");
if (sM01 == 1) {
sMatt01 = true;
} else {
sMatt01 = false;
}
//Space Matt Button 02
sM02 = PlayerPrefs.GetInt ("spaceMatt02");
if (sM02 == 1) {
sMatt02 = true;
} else {
sMatt02 = false;
}
}
//Space Matt Buttons Grid
public void spaceMatt01OnOff(){ //Space Matt Button 01
sMatt01 = ! sMatt01;
if (sMatt01 == false) {
PlayerPrefs.SetInt ("spaceMatt01", 0);
} else {
sMatt01 = true;
PlayerPrefs.SetInt ("spaceMatt01", 1);
}
}
public void spaceMatt02OnOff(){ //Space Matt Button 02
sMatt02 = ! sMatt02;
if (sMatt02 == false) {
PlayerPrefs.SetInt ("spaceMatt02", 0);
} else {
sMatt02 = true;
PlayerPrefs.SetInt ("spaceMatt02", 1);
}
}
To better help illustrate what I'm wanting to do I have attached the following image of my UI
Answer by FortisVenaliter · Jan 27, 2016 at 07:27 PM
Yes. You need to store the active index and update the display to respond.
When a user presses a button, switch to the index of that button. When disabled, use something like -1 to indicate nothing is active.
When the index changes, pull the appropriate text for that index and load it into the textfield.
When the text is changed and the index is valid, store the value of the textfield to your data.
That should get you started. Let me know if anything is unclear.
Thanks for responding @FortisVenaliter, For my "Unnamed Button" text I am just using a single UI text object I'm hoping to update when that button is selected. Can you explain a little more in depth what exactly I would need to do. If the first couple of buttons have the format I can learn best from that example.
Well, first, you need to deter$$anonymous$$e how you are storing the actual String objects for the text of each button. Pick a data structure, write the code to store it, and show that here, and then I can provide more details.
@FortisVenaliter, $$anonymous$$y thought was something like this from the script reference page:
PlayerPrefs.SetString("Player Name", "Foobar");
No, don't use bools at all. Use an integer that stores the selected index, if there is one, or -1 if there isn't. That way you don't need a separate variable for each button, and you can just check if it's >= 0 to see if a button is selected.
Then, you can have a single function as well. The int index parameter would contain the button's number when it's called, so it can be used for any button. You could also have a Button[] array and then you don't need to have separate references for each button; it would just be the button in the array at the specified index.
And, you'll want to use PlayerPrefs.SetString/GetString if you're storing text. Just use the index to generate the PlayerPrefs key, such as:
String key = "space$$anonymous$$att"+index.ToString("00");