# How to have random and different colors on multiple objects with one material?

I want to have one material applied to multiple objects that has a random color on each object. How can I achieve this so that I don't need to use a different material for every object? This is in Unity 5 by the way.

• What is your code environment, platform etc? – madneon Nov 23 '16 at 3:36
• Good point, I'll add it to the description – mr-matt Nov 23 '16 at 4:22
• Vertex colours, texture atlassing, mutliple UV channels, worldspace colouring, material property blocks, instanced rendering, lots of options. What kinds of objects are you colouring, and what do they need to do? – DMGregory Nov 23 '16 at 4:58
• Those are some good ideas thanks. The transom colors after for the shirts a group of 100+ people so I don't want them all to have the same shirts. – mr-matt Nov 23 '16 at 5:13
• Possible duplicate of How can I change the color of an object at runtime? – Philipp Nov 23 '16 at 9:33

You can change the color of the same material, on different objects, from script. Do the following:

1. Create a materialand assign it to the target game object.

2. Create a new C# script, and copy in the following code. Inside "parent" the objects whose color should change is placed in the unity editor.

using UnityEngine;
using System.Collections.Generic;

public class RandomColor : MonoBehaviour
{
public Transform parent;

List<Transform> objectstoColor;

void Start ()
{
objectstoColor = new List<Transform>();

foreach (Transform item in parent)
{
}
}

<summary>Method for changing the material color</summary>
public void SwitchColors()
{
foreach (Transform item in objectstoColor)
{
Renderer rend = item.gameObject.GetComponent<Renderer>();
rend.material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
}
}
}


• Changing renderer.material.color is a quick but dirty solution. A better way to do this is with a MaterialPropertyBlock. I marked this question as a duplicate of another question where I posted an answer which explains why that solution is better and how to do it in detail. – Philipp Nov 23 '16 at 9:34
• Thanks philipp for that information. I am not familiar with that. I shall see that for sure. – Shuvro Sarkar Nov 23 '16 at 10:52
• @Philipp your solution looks really good. How can I do that for multiple materials? I have 5 materials on each character, each one changing at runtime. Can that be done with your way? – mr-matt Nov 24 '16 at 23:00
• @Gnemlock That shouldn't be the case with the way it's written here. Accessing the .material getter creates a copy of that material unique to that Renderer instance. (See the docs: "Modifying material will change the material for this object only. If the material is used by any other renderers, this will clone the shared material") You may be thinking of .sharedMaterial, which returns a reference to the current material without copying it. – DMGregory Dec 25 '16 at 4:35