GetComponentInChildren< Renderer > failing to find the renderer

[1216-GetComponentInChildren.unitypackage.zip|1216]I 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.

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.