Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 p_unity · Jan 26, 2015 at 08:05 PM · getcomponentparentcomponentroot

Access component of parent with multiple children

Ok. So the string component i want to access is on the parent gameObject. However the child gameObejct i am trying to acces if FROM has a parent of its own. This is the hierachy of my gameObject

alt text

I need to access a component on the "Inhabitant" from a script on the text property of the MoreStats.

I have tried using transform.root to try and get the topmost transform in the hierachy howver its till returning null reference..

heres how im trying to access and assing the string

 public string state;
     Text stateText;
 
     // Use this for initialization
     void Start () {
 
         stateText = GetComponent<Text>();
 
 
         state = transform.root.GetComponent<InfectionModel>().stateOfInfection;


and the "state" exists in the inhabitant like so

 public string state;

So when accessing a parent of a parent do i need to set up a gameObejct reference too? not sure what im doing wrong.. might be a simple one but im struggling..

Thanks in advance guys

inhabitant-heirachy.png (2.1 kB)
Comment
Add comment · Show 1
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 fafase · Jan 27, 2015 at 01:53 PM 0
Share

transform.root has to work or there is a bug.

Try to decompose to see when the problem occurs.

 Transform top = transform.root;
 if(top == null){
    Debug.Log("top");
    return;
 }
 Infection$$anonymous$$odel script = top.GetComponent<Infection$$anonymous$$odel>();
 if(script == null){
    Debug.Log("script");
    return;
 }
 string state = script.stateOfInfection;
 if(state == null){
    Debug.Log("state");
    return
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Jan 26, 2015 at 02:26 PM

GameObject.Find("..."); is your friend - use that to get the GameObject then GetComponent for whatever components...

EDIT: foolishly assuming it's the Text object you're after - rename the Text object to InhabitantText then

 var inhabitantTextObj = GameObject.Find("InhabitantText");
 Text inhabatantText;
 if (inhabitantTextObj != null)
 {
     inhabitantText = inhabitantTextObj.GetComponent<Text>();
     // do whatever you need to the Text component
 }

Comment
Add comment · Show 8 · 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 p_unity · Jan 26, 2015 at 02:36 PM 0
Share

Well what i need to to set the Text of the text child using a string that is accessed through the "Inhabitant".. No access the text through the inhabintant.. If you know what i mean

So access the root parent and a variable attached to that

Also worth noting that there is 200+ inhabitants prefabs in my scene all with the same name so cant really find with name or tag..

Each text needs only to access the state (Which is the string i want to display) of its parent not all gameObjects called Inhabitanat. Hope this makes sense

Cheers

avatar image gjf · Jan 26, 2015 at 02:47 PM 0
Share

if you only have one Text component then GetComponentInChildren() is also your friend...

if there's more than one then you'll need a way to differentiate, but GetComponentsInChildren() could help... note the additional 's' ;)

EDIT: depending on how you've set it up, the following might also do it

 inhabitantText = transform.Find("InhabitantStatsCanvas/$$anonymous$$oreStats/Text");

which isn't a million miles away from what you had...

avatar image p_unity · Jan 26, 2015 at 02:53 PM 0
Share

Im not sure you understand what i want to do here.. I dont want to access the text, i want to access the "Inhabitants" (The parent) infection script from the text (Which is the child, but also has other parents) i dont need to access the child object or the Text.. I need to access the infection script THROUGH the a script on the text. I have tried this.. but still get a null reference

 public GameObject inhabitantParent;
     private string state;
     Text stateText;
 
     // Use this for initialization
     void Awake () {
 
         inhabitantParent = transform.root.gameObject;                                // get the root parent!
 
         state = inhabitantParent.GetComponent<Infection$$anonymous$$odel>().state;
 
         stateText = GetComponent<Text>();
         
     
     }
     
     // Update is called once per frame
     void Update () {
 
         stateText.text = "Current Status: " +state ;
     
     }
 }


This script is attached to the TEXT child

avatar image gjf · Jan 26, 2015 at 03:05 PM 0
Share

i misunderstood. i didn't realize that this script was on the Text component when reading this:

"the string component i want to access is on the parent gameObject"

without seeing the inspector for the various objects it's tough to tell.

is there a reason why you're controlling it from that end - couldn't you handle everything from 'Inhabitant'? i guess i don't fully understand the purpose of 'Text' - i'd (foolishly!) assumed that it was a just an element to display something - text perhaps ;)

what do you mean by "has other parents"? how are you setting them?

avatar image p_unity · Jan 26, 2015 at 03:18 PM 0
Share

I posted a pic of the inhabitant hierarchy in my original post. The text is already a child of the "statDiplay" which is a child of the world space canvas, which is a child of my inhabitant.

The text is just a display text yes.. I did think maybe would be easier to access the text through the inhabitant. So from set child from parent, not parent from child.

Is that te only way around?

Still doesn't get to why the transform.root is not working like it should and I'm still getting a null reference

Show more comments
avatar image
0

Answer by tanoshimi · Jan 27, 2015 at 01:03 PM

To access a component attached to "Inhabitant" from "Text":

 transform.parent.parent.parent.GetComponent<WhateverScriptOnInhabitantYouWant>();

or:

 transform.GetComponentInParent<WhateverScriptOnInhabitantYouWant>();
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 gjf · Jan 27, 2015 at 01:11 PM 0
Share

doesn't the latter only work for the immediate parent?

avatar image Derek-Wong · Jan 27, 2015 at 02:16 PM 0
Share

i prefer GameObject.Find

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

2 People are following this question.

avatar image avatar image

Related Questions

Better to assign components to variables or reference directly each time 3 Answers

2D Animation does not start 1 Answer

AddComponent for RawImage not functioning as expected 0 Answers

How to activate parent camera of gameobject -1 Answers

Reference Not Set toInstance of Object 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