Same color on all child GameObjects instead of different colors with function
I'm making a scene in VR and I'm having a problem with a GameObject and its children.
So I have a ladder made from 13 parts, and I'm using VRTK to make it grabbable, and I have this function being called whenever I'm holding the ladder and also pressing the trigger button on the rift controller:
 namespace VRTK.Examples
 {
     using UnityEngine;
 
     public class Color2 : VRTK_InteractableObject
     {
         Color[] colors = new Color[6];
 
         void Start()
         {
             colors[0] = Color.cyan;
             colors[1] = Color.red;
             colors[2] = Color.green;
             colors[3] = new Color(255, 165, 0);
             colors[4] = Color.yellow;
             colors[5] = Color.magenta;
         }
 
         public override void StartUsing(VRTK_InteractUse usingObject)
         {
             base.StartUsing(usingObject);
             LadderUse();
         }
 
 
         private void LadderUse()
         {
             GameObject[] objects = GameObject.FindGameObjectsWithTag("Ladder");
             foreach (GameObject go in objects)
             {
                 MeshRenderer[] renderers = go.GetComponentsInChildren<MeshRenderer>();
                 foreach (MeshRenderer r in renderers)
                 {
                     foreach (Material m in r.materials)
                     {
                         if (m.HasProperty("_Color"))
                             m.color = colors[Random.Range(0, colors.Length)]; ;
                     }
                 }
             }
         }
     }
 }
 
               And my problem is that every part of the ladder gets a different color, what should I change to make them all get the same color instead? I'm pretty new to c#, unity and VRTK but everything is working like planned except this annoying part! I really appreciate if someone could help me out :)
One solution I can think of is to make all of the ladder child objects into one object instead, but I also don't know how to do that and I have ofc tried googling but I don't even know what to google in the first place
Answer by Dragate · Nov 24, 2017 at 01:08 PM
You've put random inside a loop. Move it out to get one random index and reuse it for all iterations.
      private void LadderUse()
      {
          GameObject[] objects = GameObject.FindGameObjectsWithTag("Ladder");
          int randomColorIndex = Random.Range(0, colors.Length);
          foreach (GameObject go in objects)
          {
              MeshRenderer[] renderers = go.GetComponentsInChildren<MeshRenderer>();
              foreach (MeshRenderer r in renderers)
              {
                  foreach (Material m in r.materials)
                  {
                      if (m.HasProperty("_Color"))
                          m.color = colors[randomColorIndex];
                  }
              }
          }
      }
 
              Your answer
 
             Follow this Question
Related Questions
Antichamber-like shader for lights and outline? 0 Answers
SteamVR Plugin and 3D-technology don't work with each other 0 Answers
Mesh Cutting Metal with jaws-of-life 0 Answers
OVR Walking Animation trigger. 0 Answers