- Home /
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
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
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
}
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
}
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
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...
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
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?
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
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>();