Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question
    
Wouldn't something like gameobject.material.renderer.color = color.somecolor work? – Felsir Feb 12 at 13:37
    
@Felsir That creates material instances so while they use the same asset the objects won't batch. – Draco18s Feb 12 at 15:09
up vote 6 down vote accepted

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.

share|improve this answer

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).

share|improve this answer
    
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). – Draco18s Feb 12 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.

share|improve this answer
2  
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. – Hamza Hasan Feb 12 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. – Draco18s Feb 12 at 15:20
    
"I'm new to Unity" I always consider the level of asker on the specific platform... – Hamza Hasan Feb 12 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. – Draco18s Feb 12 at 15:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.