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
2
Question by primus88 · Aug 31, 2013 at 04:45 PM · componentchildrenaccessing

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

Comment
Add comment · Show 4
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 aldonaletto · Aug 31, 2013 at 07:01 PM 0
Share

Could you have other "Tutorial" objects in scene? If so, maybe the wrong one is being returned by Find

avatar image primus88 · Aug 31, 2013 at 07:07 PM 0
Share

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?

avatar image Hagane · Dec 03, 2013 at 03:41 AM 0
Share

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 ?

avatar image Bunny83 · Dec 03, 2013 at 03:46 AM 0
Share

@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

3 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

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.

Comment
Add comment · Show 4 · 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 primus88 · Aug 31, 2013 at 07:18 PM 0
Share

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.

avatar image Bunny83 · Dec 03, 2013 at 03:49 AM 1
Share

@primus88: That's why i usually preach to use Awake to initialize the class itself and use Start to setup links between different objects.

avatar image Bryan-Legend · May 29, 2014 at 07:45 PM 0
Share

It also fails when the gameObject is not active.

avatar image saruul34 · Jan 06, 2015 at 03:49 AM 0
Share

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

avatar image
1

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

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
avatar image
1

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";
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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

when i access a variable from another script, it doesn't change the variable in that script. what am i doing wrong? 0 Answers

cant add script 1 Answer

Activate children 2 Answers

How do I store the colors of children in an array? 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