Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by doubleunity · Apr 08, 2016 at 07:18 PM · gameobjectgetcomponent

How to get a single Text component from one of multiple children all containing Text components

Fairly self explanatory; I'm trying to getcomponent a specific (solitary) Text component from a gameobject containing multiple children, all of which have their own text component.

Any help appreciated.

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

Answer by Jessespike · Apr 08, 2016 at 07:32 PM

There are many ways.

1) You could set up a public reference in the inspector instead of using GetComponent:

  public Text mText;

2) Almost the samething as method 1, but use a serialized private reference:

 [SerializeField]
 private Text mText;

3) Or if the text component has a unique name, then you could do a search for it:

   Text mText = GameObject.Find("MyUniqueTextName").GetComponent<Text>();

4) You could also create a dummy script that you can search for with GetComponent or GetComponentInChildren:

   public class UniqueIdentifierSample : MonoBehaviour {
   }

then in your script:

  Text mText = GetComponentInChildren<UniqueIdentifierSample>().GetComponent<Text>();

5) Let the component set itself to the parent:

  GetComponentInParent<MyScript>().mText = this;

There are more ways, such as using a singleton manager class to stores the references, but these examples should be enough. I think the 1st method is probably the easist solution. Just make a public reference and set it up in the inspector.

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 doubleunity · Apr 08, 2016 at 08:29 PM 0
Share

Couldn't ask for a more comprehensive answer. Thank you.

avatar image
0

Answer by StandAloneStudio · Apr 13, 2020 at 11:27 PM

Зашел в поисках ответа, но все это меня не устроило и вот решение которое мне действительно подошло GameObject.transform.GetChild(0).GetComponentInChildren().text;

GetChild(0) - здесь мы обращаемся к Дочернему предмету под номером 1 в списке иерархии, изменение цифры дает возможность выбрать нам конкретный Дочерний объект.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent vs AddComponent 3 Answers

C# GetComponent Issue 2 Answers

Acessing a Script Instance from Another Object 3 Answers

What is the difference between GameObject.GetComponent and Component.GetComponent? 1 Answer

How do I change Sprites (and manipulate the Sprite Object) in C#? 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