Hi,
I’m having some problems with layers in my game objects. I have a game object called SkyCylinder and it have a child game object called Mesh. I’m trying to change the layer on the Mesh object from script but only the SkyCylinder object get altered.
How can I access a child game object? Currently I’m doing like this:
MeshRenderer renderer = m_GameObject.GetComponentInChildren(typeof(MeshRenderer)) as MeshRenderer;
if (renderer != null)
{
jb_Debug.Info("renderer.gameObject.name=" + renderer.gameObject.name);
renderer.gameObject.layer = m_Layer;
}
The output is saying that the game object name is “Mesh” but when I change the layer only the parent game object is changed.
Thanks!
//ohm
That’s because, in effect, that is exactly what you’re doing. m_GameObject and renderer.gameObject are one and the same!
MeshRenderer renderer = m_GameObject.renderer;
GameObject gameObject = renderer.gameObject;
if (gameObject == m_GameObject)
{
Debug.Log("Aha!");
}
You can’t change the layer property on the renderer itself, since the renderer doesn’t have that property (the GameObject does).
What were you trying to accomplish?
When I load an fbx into a game object I get a hierarchy like this:
SkyCylinder (GameObject and root object)
Mesh (GameObject I think)
Renderer (Component)
Other (Components)
I want to change the layer on the “Mesh” object. And I think I got it to work but I’m not sure until I test on another computer. Sometimes the meta data stored locally on my computer makes things work.
//ohm