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 /
  • Help Room /
avatar image
0
Question by SoCentral2 · Oct 19, 2015 at 02:37 PM · getcomponentscriptingbasicsobjectsgameobjects

How would I access a property of another objects child component?

In my Unity 5 project I have a controller script. I want to write code to change the text in the text property of the Text object which is the child of the UI Button object. My controller script is not attached to anything.

My head is spinning from reading about GetComponent, GameObject.Find, and Transform.find.

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

Answer by SoCentral2 · Oct 19, 2015 at 04:52 PM

Thanks for that, I can almost smell victory.

I'm not quite sure what this is about ...

 var button : Transform

I'm guessing it's JavaScript and I'm using C# or maybe my Visual Studio is being fussy.

Anyway, I'm trying this ...

 var button = GameObject.Find("buttonName").transform;
 button.GetChild(0).GetComponent(Text).text = "1";

but I'm getting an error on the Text of GetComponent(Text) "Text is a type which is not valid in the given context". I get the same with ...

 Transform button;
 public void doButtonThing()
 {
 button = GameObject.Find("buttonName").transform;
 button.GetChild(0).GetComponent(Text).text = "1";
 }



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 Xephex · Oct 19, 2015 at 05:58 PM 0
Share

It was JavaScript. Sorry, I am not good with C#, but I managed to get it to work. Like this:

         button = GameObject.Find("buttonName").transform;//Same as JavaScript
         button.GetChild(0).GetComponent<Text>().text = "Text to change to"; //You need to use more than and less than signs ins$$anonymous$$d with C#, also be sure to include the parentheses after them.

But the most important thing for you to do is, at the top of your script, below "using System.Collections, place this line:

 using UnityEngine.UI;
 

And when I did var button : Transform; You should have done:

  Transform button;

Answer was updated.

avatar image SoCentral2 Xephex · Oct 19, 2015 at 06:09 PM 0
Share

Yep, that worked perfectly, thank you.

 using UnityEngine.UI;

was yesterday's $$anonymous$$i-drama.

avatar image
2

Answer by Xephex · Oct 19, 2015 at 03:56 PM

GameObjects do not hold information about parents or children. To get information about this you have to get the objects Transform. So you could very simply use:

 var button : Transform = GameObject.Find("PutButtonNameHere").transform;
 
 var childOfButton = button.GetChild(0); //Gets the object at the top of the list of the button's children.
 

If you would like to check how many children something has you could do:

 var numberOfChildren = button.GetChildCount; /*returns an integer, which will tell you how many children the object has.*/

So to change the Text in the child object you would do:

 button.GetChild(0).GetComponent(Text).text = "Your Text";
 

Hope this was what you wanted.

References: http://docs.unity3d.com/ScriptReference/Transform-parent.html http://docs.unity3d.com/ScriptReference/Transform-childCount.html

Edit

In C#:

 button = GameObject.Find("buttonName").transform;//Same as JavaScript
 button.GetChild(0).GetComponent<Text>().text = "Text to change to"; //You need to use more than and less than signs instead with C#, also be sure to include the parentheses after them.

But the most important thing for you to do is, at the top of your script, below "using System.Collections, place this line:

 using UnityEngine.UI;
 

And when I did var button : Transform; in JavaScript, You should have done:

  Transform button;

Complete code I used was:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameController_C : MonoBehaviour {
     Transform button;
     
     // Update is called once per frame
     void Update () {
         button = GameObject.Find("Canvas").transform; //My parent was called Canvas, put the name of your parent there.
         button.GetChild(0).GetComponent<Text>().text = "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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with reading values from scripts on GameObjects in Lists 1 Answer

I can't access other scripts from the main script - c # 0 Answers

Effecting other game objects remotely 1 Answer

C# Accessing another objects script values 1 Answer

Get Component isn't working too well 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