# How to create a material with variable color

I'm new to Unity and trying to create a Carrom game. Since all the White and Black pieces, Queen basically have the same material and physical properties and texture except for their color, I'm trying to create a single material that I could assign to all 3 types of objects but could then set the material color per object type. Can this be achieved in Unity (I'm using 5.3.2)?

• Wouldn't something like gameobject.material.renderer.color = color.somecolor work? Feb 12 '16 at 13:37
• @Felsir That creates material instances so while they use the same asset the objects won't batch. Feb 12 '16 at 15:09

MaterialPropertyBlock probably what you are looking for. Just make sure to create the block once and cache it, to save on garbage collection.

I'd create one MaterialPropertyBlock per piece color (so a red and a black, with the material by itself being white), cache them, and apply the block to each piece as needed.

In a simple case you can change the main albedo color of the material of an object by assigning a different Color to

gameobject.renderer.material.color


Note that some advanced materials have multiple color properties which serve different purposes. In that case Material.color might not be the color you actually want to change and you have to use the material.SetColor(propertyName, color) method.

Also, in some cases a renderer can use more than one material. In that case renderer.material will be the first material. When that is not the one you want to change, you can find the other materials in the renderer.materials array.

By the way: Changing the color of the material of an object at runtime causes an entirely new material to be created. So when you have a large number of colored objects in your game but only a small number of different colors, it might be far more efficient to have a small number of different materials and assign these to the objects (as in Hamaz Hasan's answer) or use a MaterialPropertyBlock (as in the answer by Draco18s).

• This actually creates multiple materials at runtime, so not technically what the asker is looking for. Runtime material instances won't batch, which means more draw calls which for mobile devices is kind of important (I do not know the asker's requirements, but these are the downsides to utilizing this solution). Feb 12 '16 at 15:12

Actually normally people get benefit and consider it as a feature of Unity. For example, in Carrom you'd probably have to have 3 colors (Black, White and Red). What you have to do is to create 3 Materials for the above colors and apply at all 3 types of object. So from that you can change a single color from material and all the objects would change their color having this material attached.

Although you can set same physical properties to all the objects.

• My request to person who down-vote my answer, either post your correct answer or point me out where I am wrong. So that I'd know the wrong point. I'd consider that be a highly appreciable act. Feb 12 '16 at 14:52
• There is MaterialPropertyBlock, but I would ordinarily consider your answer correct as it's a much more direct solution. MaterialPropertyBlock is better used when you want to change a material property on a per object basis over time. Feb 12 '16 at 15:20
• "I'm new to Unity" I always consider the level of asker on the specific platform... Feb 12 '16 at 15:32
• Oh, sure. Which is why I think your answer is a good one. "Just use multiple materials" is easy, cheap, and good for new users. Feb 12 '16 at 15:36