Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by EsoEs · Nov 07, 2014 at 02:41 AM · guitext4.6

How to change the text of a Button with C# in 4.6

I've searched everywhere for this and I can't find an answer. I am instantiating a set of 17 different buttons, and I want to be able to edit the text field of those buttons using C# code.

 for(int i = 1; i<7;i++)
         {
             for(int k=1; k<4; k++)
             {
                 GameObject createdbutton = (GameObject)Instantiate(button);
                 createdbutton.transform.parent = this.gameObject.transform;

This successfully creates the buttons, but how do I now change the text field of each button, what is the correct command?

I am quite new to Unity and C# so please be very clear, thanks!

Comment
Add comment · Show 2
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 CanisLupus · Nov 22, 2014 at 03:37 PM 0
Share

If Uldeim's answer solved your problem and there are no better answers, remember to accept it by pressing the checkmark below the vote up/down buttons of the question :) This is so that other people will see your question as "solved".

avatar image RuggedList17 · Apr 20, 2017 at 10:00 PM 0
Share

This is one of the best forums I have seen! The answer was given perfectly and in a not complicated way. Amazing Job! I love seeing this!

4 Replies

· Add your reply
  • Sort: 
avatar image
13

Answer by Uldeim · Nov 07, 2014 at 03:33 AM

The new controls in 4.6 are pretty cool, but they're also kind of weirdly designed. The Text element of all uGUI buttons is actually a separate, child element of the button itself. Try the following code:

  createdButton.GetComponentsInChildren<Text>().text = "New Super Cool Button Text";
Comment
Add comment · Show 2 · 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 Kiwasi · Nov 07, 2014 at 04:01 AM 0
Share

The reason for this weirdness is the graphics component. Internally the engine calls rendering once on each GameObject. Allowing multiple graphics components would make the engine less efficient. Since 99 percent of the time there is no need for this buttons get shafted. As a button requires two graphics components it must have two GameObjects.

Also worth checking you have using UnityEngine.UI at the top of your script

avatar image EsoEs · Nov 07, 2014 at 10:12 PM 0
Share

Thanks so much for your reply, very well put!

avatar image
10

Answer by cashif · Oct 23, 2015 at 07:33 PM

I think it must not be GetComponentsInChildren, but GetComponentInChildren. "s" makes the difference.

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 Heroesflorian · Aug 09, 2017 at 11:15 PM 0
Share

GetComponentsInChildren() will return an Array, a T[] to be exact, containing all components of type T on all child objects.

GetComponentInChildren() will return single one (the first) component of type T that was found on any of the child objects.

Both work, which one to pick depends on the situation, in this case you only need/want 1, so "without s" would be slightly more appropriate.

avatar image
3

Answer by sfebrigade · Jun 18, 2016 at 05:46 AM

createdButton.GetComponentsInChildren().text = "New Super Cool Button Text";

This doesn't work. GetComponent() has no defintion for "text".

Comment
Add comment · Show 2 · 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 MisaelGaray · Oct 31, 2016 at 07:29 PM 0
Share

Is GetComponentInChildren in Singular. I guess the above example returns a list of Text components

avatar image Heroesflorian · Aug 09, 2017 at 11:20 PM 1
Share

Try either...

 createdButton.GetComponentsInChildren<Text>()[0].text = "New Super Cool Button Text"

...to access the text of the first element of the Text[] (Array of Text-Components) returned by GetComponent*s*, or...

 createdButton.GetComponentInChildren<Text>().text

The type parameter is required to make it work either way, and the script you place it all in needs to be aware of the Text class in the first place, i.e. either import the UI namespace...

 using UnityEngine.UI;

...or (not recommended) specify it when you use the Text...

 GetComponentsInChildren<UnityEngine.UI.Text>().text

That should fix it.

avatar image
0

Answer by FalsAlarm · Apr 21, 2017 at 08:24 AM

I think the best way to handle this situation is instead to keep an array of your button objects. First create an empty array of the same type as your Button.

Then, in your loop when you create the buttons, you might want to just push the button onto the end of the array right after you create it.

then, later on, in your event handler, or in the update function you could iterate over your button objects and set the .text property on them to be whatever you want(assuming you want them all to be the same text, then this will work fine).

However, if you wanted each button to have it's own text, then it could still be done. back when you pushed the item onto the array you could create some kind of unique identifier variable, perhaps the variable could be called realName. you could assign each button a realName and then later use the variable to identify it. @EsoEs

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

10 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

Related Questions

v4.6 Create GUI Elements Via Script? 1 Answer

Text wobbles as Content changes 2 Answers

Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 Answers

4.6 GUI Button inside a scrolling Text Box 0 Answers

How do I get my GUI text to open on awake? 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