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
1
Question by LunguM · Jan 27, 2017 at 09:38 AM · textcanvasbuttons

How to change the text of a button with a name from a list without the name repeating?

I've been attempting to change the text of each button I have in my scene with that of pre-existing GameObjects that carry a specific tag that designate locations where the user can go to.

     public Button[] bTons;
     public List<GameObject> sTor = new List<GameObject>();

I've created the list and array for the gameobjects / buttons I would be using and I successfully added them to their respective list. However, when it comes to changing the text of the Buttons I am unable to figure out how to tell each button that they must take one element from the sTor list and change their text to the name of the element they picked. That means that once one of the elements in the list has been take by one button the next button should not take that element as well but go to the next.

I've attempted to do this with a foreach loop mixed with another foreach loop and had no success.

         foreach (Button button in bTons)
         {
             var txt = button.gameObject.GetComponentInChildren<Text>();
             foreach (var list in sTor)
             {
                 txt.text = list.name;
             }
         }

I've also attempted to go through the list with a for and had the same result: All buttons within the array had their text changed to just the first element in the list, they had completely ignored the second, third, fourth and so on element.

Any help would be greatly appreciated. Thank you.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tespy · Jan 27, 2017 at 04:43 PM

You just needed to use a plain old for loop.

 for (int i = 0; i < buttons.Length; i++) 
 {
     Text buttonTextComponent = buttons[i].GetComponentInChildren<Text>();
     //^the .gameObject was unnecessary
 
     buttonTextComponent.text = locationObjects[i].name; 
     //and there we go! button text changed to the proper name!
             
 }


The problem with your foreach loop was how it didn't consider the positions of the objects in the containers (as foreach loops tend to do). Though if you still wanted to use a foreach loop, you'd need to use an outside counter variable, like so:

 int index = 0;
 foreach (Button button in buttons)
 {
     Text buttonTextComponent = buttons[index].GetComponentInChildren<Text>();
 
     buttonTextComponent.text = locationObjects[index].name;
     index++; //make sure to update the counter!
 }

Also note how I used the name "buttons" for bTons, and "locationObjects" for sTor. I can respect making variable names short, but it's best when the shortness doesn't make the names too vague.

Hope this helps! :)

Comment
Add comment · Show 1 · 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 LunguM · Jan 29, 2017 at 09:44 PM 0
Share

Oh I did not consider that. Thank you for the help and sorry for the late reply. And I will keep in $$anonymous$$d about not making the names too vague, I usually make them very descriptive, this time I let my frustration do the na$$anonymous$$g :D

avatar image
0

Answer by LunguM · Jan 27, 2017 at 10:13 AM

After fidling around more I somehow found a solution to my problem, though I have the feeling that it will bite me in the end in performance.

         for (int u = 0; u < bTons.Length; u++)
         {
             for (int i = 0; i < sTor.Count; i++)
             {
                 var txt = bTons[u].GetComponentInChildren<Text>();
                 switch (u)
                 {
                     case 0:
                         txt.text = sTor[0].name;
                         break;
                     case 1:
                         txt.text = sTor[1].name;
                         break;
                 }
             }
         }

I'm sure people will be able to tell that if I want to add more buttons (lets say 10) I'd have to hardcode all of them from the list to get the desired effect.

If anyone stumbles upon this and can provide a much clearner solution please do, I'd be more than thankful, but for the moment it seems like I've found a solution on my own.

Comment
Add comment · 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

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

67 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

Related Questions

UI Relative to screen size 1 Answer

Missing canvas elements on Build & Run. 2 Answers

My code will not change Canvas Text at all [No errors] 3 Answers

How to change button text via script? 2 Answers

UI Text problem 1 Answer


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