Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 Cromfeli · Dec 19, 2014 at 02:48 PM · arraytextgetcomponentrecttransformfindchild

How to FindChild RectTransform and access its Text

Im trying to parse my hierarchy and find 3 different labels with a text component on each of them:

 labelText = buttonObject.transform.FindChild("Firstname").GetComponent<Text>();
 labelText.text = userContactsArray[0][i];
 labelText = buttonObject.transform.FindChild("Lastname").GetComponent<Text>();
 labelText.text = userContactsArray[1][i];
 labelText = buttonObject.transform.FindChild("Description").GetComponent<Text>();
 labelText.text = userContactsArray[2][i];

But I get error "NullReferenceException: Object reference not set to an instance of an object" as if it does not find the object at all.

Is there difference with RectTransform and transform or why exactly I can not find the object? I tried something like RecTransform.FindChild but without luck. Also in documentation it was saying like (RectTransform)transform.FindChild but it didnt work either.

Thanks in advance :)

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

Answer by Mmmpies · Dec 19, 2014 at 03:05 PM

Flying blind as I don't have access to Unity but null reference means it can't find a reference to the object. Where are you setting the buttonObject or userCOntactsArray?

also you can get the child object by number so

 transform.GetChild(0).GetComponent<Text>();

or you could just set transform.

 GetChild(0).GetComponent<Text>().Text = userContactsArray...

edit: Man am I ever gonna remember to code with

 <Text>

in it in code to stop ot from vanishing!

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 Cromfeli · Dec 20, 2014 at 08:55 AM 0
Share

I did the check so that the objects do exists with buttonObject and userContactsArray. But just when I try to use Find with a RectTransform it return null.

I will try to make repro file to show it.

avatar image Mmmpies · Dec 20, 2014 at 04:16 PM 0
Share

I've now got better Internet access! If you look up the API for RectTransform here then there is no FindChild just RectTransform.Find for looking up the name as you do, or RectTransform.GetChild for looking up via the int index of the child.

Guess they changed the name for RectTransform, or, having just looked FindChild may be deprecated as it no longer appears in Transform either.

Hope that helps fix the issue for you.

avatar image
0

Answer by tothemario · Dec 04, 2019 at 08:20 AM

RectTransform inherits from Transform. Navigating the hierarchy with transform.Find is the same.

I think transform.Find is working well, but GetComponent() is returning null because probably the component you are using in the game object is a , or maybe a

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need an advise, how to find all objects with a specific name and a specific script? 2 Answers

Using getcomponent with an array 2 Answers

DIALOGUE BRANCH CREATION? 1 Answer

Create multiple math problems with arrays? Help with arrays 1 Answer

Problems to change text from a child in a child 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