- Home /
Why GetComponentInChildren is not working ?
Hello,
I have a script named CloseTutorialTower located on Quit child.
OtherParents
=> Tutorial
=>=>InGameTutorial
=>=>=>RPGDialog
=>=>=>=>Quit
I use the following code to access the script :
GameObject rpgDialog = GameObject.Find("Tutorial");
Debug.Log(rpgDialog);
CloseTutorialTower zaScripts = rpgDialog.GetComponentInChildren<CloseTutorialTower>();
zaScripts.OnClick();
But, even though it find the object Tutorial, it does no find the script on the childrent of Tutorial.
What am I doing wrong ?
Thanks
Could you have other "Tutorial" objects in scene? If so, maybe the wrong one is being returned by Find
I have only one Tutorial object. The thing is that the children of Tutorial are created each time the scene starts by script. Could this be the problem ? I thought that Start() runs after all the objects in the scene were awekened, no?
Hey,
I repost here, because i have a similar problem :
Debug.Log(_enemiPrefab.transform.GetComponentInChildren<Renderer>()); --> "Null"
Debug.Log(_enemiPrefab.transform.FindChild("Skin").renderer); --> (UnityEngine.$$anonymous$$eshRenderer)
Why it isn't working ?
@Hagane: Don't post questions as answers. This is not a forum. One question - multiple answers. Answers should answer the question.
I've converted your answer into a comment.
Well you reposted your problem... Are you sure that it's not the problem described by "Paulius Liekis"?
http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentInChildren.html
Answer by Paulius-Liekis · Aug 31, 2013 at 06:47 PM
Maybe that script is disabled? You have to pass true to GetComponentInChildren if you want to get disabled components.
I think you are correct. I found out that the issue was because that the child I was looking for was awakened in the scene after I was perfor$$anonymous$$g the search for the script component.
I just moved it in the code, just after I was awekening the child.
@primus88: That's why i usually preach to use Awake to initialize the class itself and use Start to setup links between different objects.
actually you may try GetComponentsInChildren(true) https://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponentsInChildren.html there is a boolean parameter allow you to include inactive objects
Answer by citizen_rafiq · Aug 31, 2013 at 07:36 PM
GameObject rpgDialog = GameObject.Find("Tutorial");
Debug.Log(rpgDialog);
CloseTutorialTower zaScripts = rpgDialog.GetComponentInChildren<CloseTutorialTower>();
if(zaScripts) zaScripts.OnClick();
//good luck
Answer by downpour · Sep 15, 2014 at 06:18 AM
Since GetComponent() works on inactive objects, you can also get the transform's child first and then use GetComponent()
yourGameObject.transform.GetChild(0).GetComponent<TextMesh>().text = "Your Text";
Your answer
![](https://koobas.hobune.stream/wayback/20220613113333im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
cant add script 1 Answer
Activate children 2 Answers