I am a newcomer to Unity and I am trying to create a scene. I don't know how to change the color of the objects in my scene. I've read some things on materials and meshes, but i don't get how they relate to the actual color of the object.
Answer by whydoidoit
Jan 28, 2013 at 02:18 AM
So the mesh is the shape of your object and the material is the settings for how that shape will be drawn.
Normally when you want to change the color of something you:
Create a new material
Choose a shader
Set the color/textures required by the shader
A simple shader like Diffuse (the default) will take a color and a texture - more advanced shaders may have many colors and many textures.
Via code you can change a color of a single object by doing:
renderer.material.color = new Color(0.5f,1,1); //C#
renderer.material.color = Color(0.5,1,1); //JS
please help me how to change its background not color. Thanks
For the code example use the appropriate Component of a GameObject instead, it is gameObject.GetComponent().material.color = new Color(0.5f,1,1);
Answer by ShawnFeatherly
Dec 24, 2013 at 01:15 AM
For solid colors, you can create a script that can be added to gameobjects, such as cubes, that will let you change their color:
public class ColorSolid : MonoBehaviour
public Color ObjectColor;
private Color currentColor;
private Material materialColored;
if (ObjectColor != currentColor)
//helps stop memory leaks
if (materialColored != null)
//create a new material
materialColored = new Material(Shader.Find("Diffuse"));
materialColored.color = currentColor = ObjectColor;
this.GetComponent<Renderer>().material = materialColored;
Answer by GraviterX
Dec 24, 2013 at 02:04 AM
Here is a script that changes colors on keypress. You can edit the keys and the colors. #pragma strict
function Update ()
gameObject.renderer.material.color = Color.red;
gameObject.renderer.material.color = Color.blue;
if (Input.GetKeyDown (KeyCode.T))
gameObject.renderer.material.color = Color.white;
Answer by unity3dengineer
in new version of unity, API has updated and you must use this codes:
GetComponent<Renderer>().material.color = new Color(0, 255, 0); //C sharp
Good point. I just edited my answer to work with Unity5.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
14 People are following this question.
How can I make this more efficient?
Changing two different objects renderer colour
Material doesn't have a color property '_Color'
Accessing material of particle renderer??
Adding a Color Change Script to a Model