title: Unity C# why color of prefab child does not change
body: <p>In my unity project I have a prefab with many child objects. If I click on a button, I want that some child objects from the prefab change their color. Unfortunately with my code nothing happens.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public class ColorChanger : MonoBehaviour
  {
      public GameObject preFabParent;
      public GameObject preFabChildTarget
  
      public Material newColor;
  
      Renderer[] prefabChildrenParent, prefabChildren;
  
      bool changePrefabColor;
  
      void Start()
      {
          prefabChildrenParent = preFabParent.GetComponentsInChildren&lt;Renderer&gt;();
          prefabChildren = preFabChildTarget.GetComponentsInChildren&lt;Renderer&gt;();
      }
  
      void Update()
      {
          // bool is changed by a button click:
          if(changePrefabColor) ChangeColor();
      }
  
      void ChangeColor()
      {
          // Now I want that all the child objects from the prefabChildTarget get the new color.
          foreach(Renderer child in prefabChildren)
          {
              var mats = new Material[child.sharedMaterials.Length];
              for(var j = 0; j &lt; child.sharedMaterials.Length, j++)
              {
                  mats[j] = newColor;
              }
              child.sharedMaterials = mats;
          }
      }
  }
  </code></pre>
  
  <p>What do I have to change that my code will work? Unfortunately I could not find a solution which works for me.</p>