setting child colors

I have a model pf a brain that I have constructed from a number of submeshes that I would to iterate through and set colors according to some rules that I have developed.

can anyone tell me the best way to iterate through such a structure and set the colors?

for (Transform child in transform) {
   //set colors
}

Put this code into your parent object. This will go trough every child of the parent.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

class MonoBrain : MonoBehaviour
{
    public GameObject pial;

    private Dictionary<string, Material> nameToMaterial;
    private void Start()
    {
        // Find all MeshRenderers nested under Pial
        // Assuming they only exist on "default" GameObject below "rh.pial.DK.*"
        MeshRenderer[] renderers = pial.GetComponentsInChildren<MeshRenderer>(); 

        nameToMaterial = new Dictionary<string, Material>();

        for (int i = 0; i < renderers.Length; i++)
        {
            // Find the name of the parent GO of the renderer.
            string partName = renderers[i].transform.parent.name;

            // Creates a clone of the material.
            Material partMaterial = renderers[i].material; 

            // Add name and material to dictionary.
            nameToMaterial.Add(partName, partMaterial);
        }

        nameToMaterial["rh.pial.DK.fusiform"].color = Color.red;
        nameToMaterial["rh.pial.DK.bankssts"].color = Color.blue;
    }   
}

Thanks guy’s : Thermalfusion : thats pretty much what my final code looks like :smile: thanks

1 Like