Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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 Giraffupus · Aug 07, 2015 at 11:00 AM · javascriptbuttontextbuttons

How to change button text via script?

I know this has been answered before but I have tried everything that other answers have given for the past few days without any success. I'm hoping someone can explain my fundamental misunderstanding behind this.

Here is the current iteration of my script. I've clicked and dragged a script into the Globals variable and I've clicked and dragged a button in to the ThisButton variable. The rest is obvious. I don't think I need the import UnityEngine.UI; but I've tried without it as well.

 #pragma strict
 import UnityEngine.UI;
 
 var Globals : GlobalValues;
 var ThisButton : UnityEngine.UI.Button;
 var CrewMember = 0;
 
 function Start () {
 
 }
 
 function Update () {
     
     if (Globals.Crew1[0] == 1){
         ThisButton.GetComponentsInChildren.<Text>.text = "testing";
     }
 
 }

Right now the error is that 'UnityEngine.Component.GetComponentsInChildren' is not a generic definition. But there have been plenty of other errors on other tries.

Basically, if my if statement is true, how do I change the text of my button? Thanks for any help on this.

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
14
Best Answer

Answer by allenallenallen · Aug 07, 2015 at 11:23 AM

Assuming you do have a Text component in one of the children of ThisButton, here's how you would fix it:

 ThisButton.GetComponentInChildren(Text).text = "testing";

I recommend you to check the API: http://docs.unity3d.com/ScriptReference/GameObject.GetComponentInChildren.html

And make sure you're in JS instead of C#.

Comment
Add comment · Show 3 · 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 Giraffupus · Aug 07, 2015 at 12:29 PM 0
Share

Thanks for the response.

To answer your troubleshooting points: In my hierarchy, the button does have a drop down arrow and there is a "Text" in the dropdown. It's my understanding that buttons are created with a Text child object. I am using javascript. I've read the GetComponentinChildren doc (and many others related to this problem) but haven't been able to solve why I can't get to the text component.

I changed the line to your suggestion (keeping in $$anonymous$$d the case sensitivity) and now I get the following error.

'text' is not a member of 'UnityEngine.Component[]'.

Again, thanks for your help.

avatar image Giraffupus · Aug 08, 2015 at 09:50 PM 0
Share

Ignore my above comment, finally got a chance to look at it at home and monodevelop auto fills with "GetCompenentsInChildren" before "GetComponentInChildren" which was my problem. Once I swapped it to the singular version the above worked fine. Thanks again.

avatar image AP54321 · Nov 04, 2019 at 12:56 PM 0
Share

I needed to do GetComponentInChildren<Text>() as it was giving me the error: "'Text' is a type, which is not valid in the given context".

avatar image
4

Answer by Chromag · Jun 22, 2018 at 06:36 AM

The API is Component.GetComponentInChildren<>(); https://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html

 // instantiate button from prefab
 GameObject go = (GameObject)Instantiate(Resources.Load("DefaultBtn"));
 // get button component from game object
 Button myBtn = go.GetComponent<Button>();
 // get button text component in children and set the text property
 myBtn.GetComponentInChildren<Text>().text = "my button text";
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

6 People are following this question.

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

Related Questions

Gui Button Solid 2 Answers

GUIText Problem With MENU 1 Answer

Rotating object with onscreen button 1 Answer

Quest Script Help 2 Answers

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 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