• 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
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
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
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
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() ?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Click ui text to dissapear in 3D unity 2017 first person game script for ui multiple texts?,Clicking Text away in Unity 2017 3D first person game 0 Answers

How to align text? 1 Answer

How to reference a variable in the text(script) component 0 Answers

Text UI is not being assigned 2 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