Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KnightRiderGuy · Jan 27, 2016 at 05:50 PM · c#uiplayerprefsinputfieldtextfield

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

Ui Screen Capture

screen-shot-2016-01-27-at-11953-pm.png (333.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Jan 27, 2016 at 08:15 PM 0
Share

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.

avatar image FortisVenaliter KnightRiderGuy · Jan 27, 2016 at 08:20 PM 0
Share

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.

avatar image KnightRiderGuy FortisVenaliter · Jan 27, 2016 at 08:30 PM 0
Share

@FortisVenaliter, $$anonymous$$y thought was something like this from the script reference page:

PlayerPrefs.SetString("Player Name", "Foobar");

Show more comments
Show more comments
avatar image FortisVenaliter KnightRiderGuy · Jan 27, 2016 at 09:02 PM 0
Share

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.

avatar image FortisVenaliter FortisVenaliter · Jan 27, 2016 at 09:03 PM 0
Share

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");
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to load a scene with playerprefs from a ui element 1 Answer

UI Input field that grows in size with each return line 1 Answer

UI Text Input Field Long Text String in Short Text Input field 2 Answers

How to Load PlayerPrefs from a different scene 1 Answer

How to make a Saved/Favorites system in unity? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges