How do you change the color of an obect (cube, cylinder, plane, etc)?

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.

more ▼

asked Jan 28, 2013 at 01:45 AM

jjbloon gravatar image

16 1 2 4

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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
more ▼

answered Jan 28, 2013 at 02:18 AM

whydoidoit gravatar image

54k 60 91 223

(comments are locked)
10|3000 characters needed characters left

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;
     void Update()
         if (ObjectColor != currentColor)
             //stop the leaks
             if (materialColored != null)
             //create a new material
             materialColored = new Material(Shader.Find("Diffuse"));
             materialColored.color = currentColor = ObjectColor;
             this.renderer.material = materialColored;
more ▼

answered Dec 24, 2013 at 01:15 AM

ShawnFeatherly gravatar image

61 1 4

(comments are locked)
10|3000 characters needed characters left

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;
more ▼

answered Dec 24, 2013 at 02:04 AM

GraviterX gravatar image

158 13 913 68

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jan 28, 2013 at 01:45 AM

Seen: 22754 times

Last Updated: Feb 01 at 05:50 AM