[OAFAT]How do I changer the color of a game object?

As a starting point, the is most common answer to how to set the color of an object is to use [Material.color][1]. Example:

 renderer.material.color = Color.red;

…or if the material is on some other object:

 otherObject.renderer.material.color = Color.red;

But as with many Unity features, things are both more complicated (and more flexible) than this initial simple answer would indicate. Assigning to Material.color sets the _Color properity (often labeled as ‘Main Color’ in the interface) of the shader associated with the material. But setting _Color is not a universal solution:

Whether a particular shader supports the _Color property, and how that shader support that property is totally up to the shader.

Most shaders that ship with Unity and many [shaders in the Unity Wiki][2] support a _Color property, but there are notable exceptions. For example, none of the Mobile shaders that ship with Unity have a _Color property. Here is a list of common problems encountered when using material.color to set the color of an object:

No _Color property in the shader - as mentioned not all shader has this property. Most shaders that support the property name the color ‘Main Color’ in the interface. If you want to be absolutely sure, select the material with the shader in question, and click the ‘Edit…’ button in the upper right corner. The properties of that shader will be listed on the left hand side. If you open the shader, in the properties block you will see something like:

 _Color ("Main Color", Color) = (1,1,1,1)

Wrong use of color - Unity’s Color class uses values in the range of 0.0 to 1.0 for r, g, b, and a. It is not uncommon for beginners to use values in the range of 0.0 to 255.0 like are used in many image editing programs. Note that Unity supports two color classes: Color and Color32, and these two can implicitly converted from one to the other. Color32 supports values in the range of 0.0 to 0.255 for rgba. So if needing to use values in the range of 0.0 to 255.0, this can be done:

 renderer.material.color = Color32(32.0, 105.0, 202.0);

Shader supports over-brightening - Same shaders are implemented to support over-brightening. For an accurate color, all the rgb values need to be cut in half (and placed over a white texture).

Most shaders implement color as an overlay - Overlay means that the color specified in the _Color property is combined with the any color in the texture and possibly other color properites. This means to get the color hue right, the texture/color of the object must be white or greyscale before the overlay color is applied.

Materials array - For the built-in Unity objects like cubes and spheres, there is only one material for the mesh. But imported objects may have several materials for a single mesh. For example a car model might have different materals for the tires, windows, paint, and chrome. You can figure out the size of the materials array by executing the following code on the object:

 Debug.Log(renderer.materials.Length);

If the value returned is greater than 1, then there will be multiple materials on the mesh. To change the color of the whole object, the color must be changed of the individual materials in the materials array:

for (var i = 0; i < renderer.materials.length; i++) {
    renderer.materials*.color = Color.red;*

}

Shaders that use lighting - for shaders that react to light, the intensity and color of the light will impact the color. Don’t forget the color of Ambient Light as well (Edit > Render Settings > Ambinet Light).

Dealing with colors other than _Color - The shader programmer defines the property names of any color properties in a shader. The name(s) might be _SpecColor, _Tint, _Emission and _ReflectColor. [Material.SetColor()][3] function can be used to set the value for these other colors:
renderer.material.SetColor (“_Tint”, Color.red);

In fact, assigning a value for material.color is just a short form of SetColor(), so _Color can also be set by:

renderer.material.SetColor(“_Color”, Color.red);

Changing color on objects that share a material - at runtime, when you first make any change to a material including setting the color, Unity clones the material to create a new material instance. This means that color changes (or any changes to the material) will only impact the single object at runtime. But sometimes you want to change the color for all the objects that share the material. It can be accomplished by:
renderer.sharedMaterial.color = Color.red;

Note that any changes to the sharedMaterial made at runtime in the editor will hang around after the app is stopped.

Setting the material - Sometimes, rather than change the material.color, it makes sense to change the whole material. Any color properties can be set in the Inspector on the new material, and then at runtime:
renderer.material = someMaterial;

Vertex Colors - setting the colors array in the Mesh (vertex colors) is a less common way to set the color of an object. Vertex colors are primarily used in two situations: 1) where there are multiple, low-poly count objects that share a material, and 2) if you want to change the color of part of an object (not using submeshes). Simpler objects (typically less 300 vertices) that share a material will be batched and sent to the GPU together, resulting in performance improvements. Any change to a material, such as setting the color of the material, results in a new material instance, and therefore that object will no longer batch. See [the Draw Call Batching in the manual][4] for more information on the criteria for batching. But using vertex colors allows the changing of color of objects without breaking batching. Whether a shader supports vertex colors, and how it handles vertex colors, is completely up to the shader. I am unaware of any shader that ships with Unity that supports vertex colors, but several in the Unity Wiki support vertex colors. These shaders usually have the text ‘VertexColor’ in the title. Shaders with ‘VertexLit’ usually do not support vertex colors.
Here are two references to shaders in the Unity wiki that support vertex colors:
http://wiki.unity3d.com/index.php/VertexColorUnlit
http://wiki.unity3d.com/index.php/VertexColor
And here is a very simple vertex shader that only deals with color (no texure):
Shader “Custom/Vertex Colored” {
SubShader {
Pass {
ColorMaterial AmbientAndDiffuse
}
}
}
Vertex colors are associated with the vertices of a mesh. If you apply red, blue, green, and yellow to the four color entries of a Quad and use an unlit vertex colored shader results in:
[32751-vertexcolors1.jpg_|32751]*
This is a sample function that sets all the vertex colors on an object to the same color.
function SetVertexColors(color : Color) {
var mf = GetComponent(MeshFilter);
var colors : Color[] = new Color[mr.mesh.vertices.Length];
for (var c : Color in colors) {
c = color;
}
mf.mesh.colors = colors;
}

Note that for frequent changes, the colors array and reference to the mesh can be done once in start and then reused.
This second script sets the side of a built-in cube to random colors. Put a vertex color shader on a built-in cube. Rotate the cube so you can see three sides, and add the following script. Press the space key while the script is running.
#pragma strict
private var sides = [
[0,1,2,3],
[8,9,4,5],
[10,11,6,7],
[12,14,15,13],
[16,18,19,17],
[20,22,23,21]
];
private var mesh : Mesh;
private var colors : Color[];
function Start () {
var mf = GetComponent(MeshFilter);
if (mf != null)
mesh = mf.mesh;

if (mesh == null || mesh.vertices.Length != 24) {
Debug.Log(“Script needs to be attached to built-in cube”);
return;
}

colors = new Color[24];
Debug.Log(colors.Length);
}
function Update() {
if (colors != null && Input.GetKeyDown(KeyCode.Space)) {
for (var i = 0; i < 6; i++) {
var color = Color(Random.value, Random.value, Random.value, 1.0);
for (var j = 0; j < 4; j++) {
colors[sides*[j]] = color;*
}
}
mesh.colors = colors;
}
}

Special game objects - There are a couple of game objects where color is handled a bit differenty. For GUITexture objects, the color is set by the [GUITexture.color][6]. For Sprite objects, the color is set by using [SpriteRenderer.color][7].

Alternates to setting object color - Sometimes the better solution to change the ‘color’ is to display a new texture with a different color. This can be done by assigning a new texture to [Material.mainTexture][8], or it can be done by using a texture atlas or sprite sheet and changing the Mesh.uv. It is beyond the scope of this answer to cover changing textures in detail, but [here is an answer concerning the use of texture atlases][9], and [here is a question where UV coordinates are used to map different parts of a cube to texture areas of different colors][10]. And there are many more questions on Unity Answers on changing textures, setting UVs, texture atlases, and sprite sheets.
_[1]: http://docs.unity3d.com/ScriptReference/Material-color.html*_
_
[2]: http://wiki.unity3d.com/index.php?title=Shaders*_
_[3]: http://docs.unity3d.com/ScriptReference/Material.SetColor.html*_
_
[4]: http://docs.unity3d.com/Manual/DrawCallBatching.html*_
_*
_[6]: http://docs.unity3d.com/ScriptReference/GUITexture-color.html*_
_
[7]: http://docs.unity3d.com/ScriptReference/SpriteRenderer-color.html*_
_[8]: http://docs.unity3d.com/ScriptReference/Material-mainTexture.html*_
_
[9]: http://answers.unity3d.com/questions/14578/whats-the-best-way-to-reduce-draw-calls.html*_
_*[10]: http://answers.unity3d.com/questions/542787/change-texture-of-cube-sides.html*_