Random color Help

How could I modify this script so that the random color is applied to several 3D objects, since with this script I only manage to give it a random color one by one and it is of many colors, I want to put a random color to several 3d objects, not I know if he explains me well. Any advice help, thank you…

Instead of using GetComponent() you can use GetComponentsInChildren() and you will get an array of mesh renderers, all the ones at or below the current GameObject this script is on.

MeshRenderer[] allMeshRenderers = GetComponentsInChildren<MeshRenderer>();

Then you can iterate that array. It is not guaranteed to be in any particular order.

@Fekzh21 , I am responding to your private mail (let’s keep it out here in public so everybody can benefit and learn and chip in).

You need to use a for() loop to iterate the MeshRenders, not just operate on a single one as your original code does, something like:

for (int i = 0; i < allMeshRenderers.Length; i++)
{
  // do whatever you want with each one here, such as:
  allMeshRenderers[i].material.color = ... your original code here
}
1 Like