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 cacysunlee · Apr 10, 2016 at 08:14 AM · uitextchild objectmany

How to get a Text child, when there are many Text childs

hello,

i instantiate a prefab (monster-card) to UI. this monster-card-prefab has many text fields, like name, health, attack, description, ...

now i will change the description of this card, but how acces exactly a particular Text element.

but with this i get only the first text element of this card and it changes the name :

 newText = newCard.GetComponentInChildren<Text>();
 newText.text = "new description"

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Ali-hatem · Apr 10, 2016 at 08:59 AM

you need GetComponentsInChildren not GetComponentInChildren :

     Text [] newText ;
     void Start () {
         newText = GetComponentsInChildren<Text> ();
         newText [0].text = "name";
         newText [1].text = "health";
         newText [2].text = "attack";
         newText [4].text = "description";
     }
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 cacysunlee · Apr 10, 2016 at 09:08 AM 0
Share

that is it, thank you ! :)

avatar image AyanRoy · Apr 13, 2019 at 03:15 PM 0
Share

Thank you so much, worked like a charm !

avatar image
1

Answer by JoshuaMcKenzie · Apr 10, 2016 at 08:46 AM

easiest way is to have the script have multiple public Text variables

 public Text Name;
 public Text Health;
 public Text Attack;
 public Text Description;

then you set them inside inspector in unity. now in script you can simply set Description.text. this is useful as it makes your code transparent and easier to read by other programmers

Otherwise lets say that the game object is created dynamically during runtime and you can't have those inspector values preset. then you can instead use GetComponentsInChildren(). thats with an "s". which will return an array of all Text components in the children, ordered in the same order as they were placed in the heirarchy.

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 cacysunlee · Apr 10, 2016 at 08:51 AM 0
Share

thank you for the fast answer.

yes, it is created dynamically during runtime. and now how to access the description element with GetComponentsInChildren() ?

avatar image
0

Answer by cacysunlee · Apr 10, 2016 at 08:50 AM

sorry, placeholder

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
avatar image
0

Answer by junedmmn · Oct 26, 2018 at 04:48 PM

This might be the easiest to change color of children of a gameobject, you can modify the code to suit your requirement

     public void ChangeColor()
     {   
         foreach (Text text in gameobject.GetComponents.InChildren<Text> ())
         {
             text.color = new Color(1f, 0, 0, 1f);
         }
     }

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

60 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

Related Questions

Access and modify Text within a prefab 1 Answer

Graphic problem with UI Text 2 Answers

New UI: change text 2 Answers

UGUI Find tapped character in textfield? 1 Answer

How to attach a canvas to a game object properly? 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