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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by jdwieber · Jun 11, 2012 at 03:24 PM · renderergetcomponentinchildren

GetComponentInChildren< Renderer > failing to find the renderer

link textI needed to shift the pivot of a prfab, so I created an empty game object at the position I want my pivot. Then I made the prefab a child of it. Then I turned this new construct into a prefab that I use at runtime to instantiate objects. I need to get the renderer of the actual game object and I'm using this call:

 Renderer renderer = wall.GetComponentInChildren< Renderer >();

The child object indeed has an active renderer attached, but the above call always returns null. I also tried changing Renderer to MeshRenderer with no luck. Prior to parenting the prefab, I was able to get the renderer by simply saying:

 wall.renderer

The parent object has only one component: the transform, and only one child: a wall model. The wall model has four components: Transform, MeshFilter, MeshRenderer, and Collider.

From what I see in the docs, this call should succeed. Any idea what I'm doing wrong? Is there a better, or different way to do this?

Thanks.

GetComponentInChildren.unitypackage.zip (242.5 kB)
Comment
Add comment · Show 7
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 Berenger · Jun 11, 2012 at 03:26 PM 1
Share

If the renderer is attached to the wall, why do you call GetComponentInChildren ? Should still work though ...

Is the gameObject active ?

avatar image jdwieber · Jun 11, 2012 at 03:40 PM 0
Share

Hi Berenger. Thanks for the reply. I have a ref to the parent object and the renderer is attached to it's child, so it seems natural to call GetComponentInChildren rather than first get the child and then get the renderer. At first I tried calling GetComponent() directly on the parent, but that also brought back null. How would you get the renderer?

Yes, the gameObject is active. I can see the wall being rendered, they are just all piled up on each other since I have no size data to position them. Consequently, that's why I grab the renderer. I need the bound from it.

avatar image Berenger · Jun 11, 2012 at 03:47 PM 0
Share

Could you create a project with just that issue and upload it ? I don't see any reasons for it not to work, so I need to see what's going on.

avatar image whydoidoit · Jun 11, 2012 at 03:47 PM 0
Share

And if you use GetComponentInChildren<$$anonymous$$eshRenderer>() does it work?

avatar image jdwieber · Jun 11, 2012 at 04:13 PM 0
Share

@Berenger: I attached a simple project demonstrating my issue.

@whydoidoit: I'm scripting in C# and only see the one generic function available from GameObject, so I think I'm already using the one you asked about. Please correct me if I'm wrong, or misunderstood you reply.

Thank you both for the help.

Show more comments

1 Reply

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

Answer by Berenger · Jun 11, 2012 at 04:18 PM

Ok, thanks for the package.

You're trying to access the renderer on your prefab, not the instance of the prefab. That's why you get the error. So, either replace

 Renderer renderer = wall.GetComponentInChildren< Renderer >();

by

 GameObject inst = Instantiate( wall, Vector3.zero, Quaternion.identity ) as GameObject;
 Renderer renderer = inst.GetComponentInChildren< Renderer >();

Or drag the prefab into yur scene and give that reference.

Comment
Add comment · Show 1 · 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 jdwieber · Jun 11, 2012 at 04:23 PM 0
Share

Egg on my face! I should have known that. Thanks.

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

6 People are following this question.

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

Related Questions

Access the renderer of a clones child object 1 Answer

WHy cant I create the GetComponentsInChildren(Renderer) array? 2 Answers

Changing two different objects renderer colour 1 Answer

List.Add component as Renderer, can't access? 1 Answer

How do I access the renderer from a child? 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