Hi. I’m a professional programmer and game veteran, but am with Unity3D for the first time–very new environment to learn. And I’m struggling with what is undoubtedly something very simple, and would appreciate a hand.
I’ve created a simple 3D model through AC3D, exported it as a Collada form (.dae) since that seems to import well. I constructed a prefab from this thing, and I see the prefab appears to have a structure that I did not expect:
(The model is properly formed; it’s just one-sided and you can’t see it well in this preview.) See, there’s this “disk” sub-model–a little odd, but when building the thing in AC3D I did start with a “disk” object that I then manipulated.
Anyway, if I look at the inspector for the prefab itself I see only Transform and Animator components; if I look at the inspector for the “disk” child, then I see Transform (relative to the parent; fine), Mesh Filter and Mesh Renderer children. And that mesh renderer works fine–if I assign a material to it, then when I instantiate one of these prefabs it’s all textured and pretty.
But I can’t find a way to reach that child “disk” object at runtime via a script, in order to select its material dynamically. I can instantiate a prefab:
GameObject prefab = (GameObject)GameObject.Instantiate(Factory.HexUpPrefab, ...);
…and normally I’d try to find the MeshRenderer from there:
MeshRenderer mr = prefab.GetComponent<MeshRenderer>();
But that returns null and throws a very helpful console warning: the prefab itself doesn’t have a meshrenderer, and maybe I should add one. Yes, I can indeed add a MeshRenderer to the top-level prefab and retrieve it this way, but it has zero effect on the appearance of the prefab.
What I think that I need to be doing is finding that “disk” child component, and then digging out its MeshRenderer value. But I can’t find the right way to do that. The straight-forward:
var foo = prefab.GetComponent("disk");
…just returns null, and doesn’t sound like it’s likely what I need anyway. I’m not even sure what type that disk child component should be. If I do GetComponentsInChildren I do indeed see a child transform labeled “disk”, but is that the right way to try to dig this out?