- Home /
Mesh Renderer of a Child of a Child, when the parent is Empty
Hello guys!
I got a problem with changing the colors of a complex model. It was correctly imported from and FBX. file and it has the complex structure as in the image below:
I deactivated the camera so that it doesnt appear, and as you can tell, Center_Module is the main parent, then three childs and then each has one more child (So a child of a child). I am trying to put a script on Center_module to modify the color of all the objects. This is what I'm using so far:
private bool state;
private Color[] defaultColors;
private MeshRenderer[] Comps;
void Start () {
defaultColors = getAllColors();
Comps = gameObject.GetComponentsInChildren<MeshRenderer>();
state = false;
}
public void OnInputClicked(InputClickedEventData eventData) {
if (!state) {
changeAllColors(highlight);
} else {
changeAllColors(defaultColors);
}
state = !state;
}
private void changeAllColors(Color x) {
foreach (MeshRenderer r in Comps) {
r.material.color = x;
}
}
private void changeAllColors(Color[] x) {
int i = 0;
foreach (MeshRenderer r in Comps) {
gameObject.transform.GetChild(i).GetComponent<MeshRenderer>().material.color = x[i];
i++;
//r.material.color = x;
}
}
private Color[] getAllColors() {
int i = 0;
MeshRenderer[] Comps = gameObject.GetComponentsInChildren<MeshRenderer>();
Color[] theColors = new Color[Comps.Length];
foreach (MeshRenderer r in Comps) {
theColors[i] = gameObject.transform.GetChild(i).GetComponent<MeshRenderer>().material.color;
i++;
}
return theColors;
}
If I put this script in a child, for example, Center_Base_Plate-1, it works. But if I put it on Center_Module, it just wont. It sends an error: "Object Reference not set to an instance of an object".
I used a simple model example, but I got more complicated models that have this structure, a child in a child in a child...
I have tryied many things with no success, any ideas?
Thank you!!!