x


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 '13 at 01:45 AM

jjbloon gravatar image

jjbloon
16 1 1 1

(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 '13 at 02:18 AM

whydoidoit gravatar image

whydoidoit
51.9k 23 47 158

(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:

[ExecuteInEditMode]
public class ColorSolid : MonoBehaviour
{
    public Color ObjectColor;

    private Color currentColor;
    private Material materialColored;

    void Update()
    {
       if (ObjectColor != currentColor)
       {
         //stop the leaks
         if (materialColored != null)
          UnityEditor.AssetDatabase.DeleteAsset(UnityEditor.AssetDatabase.GetAssetPath(materialColored));

         //create a new material
         materialColored = new Material(Shader.Find("Diffuse"));
         materialColored.color = currentColor = ObjectColor;
         this.renderer.material = materialColored;
       }
    }
}
more ▼

answered Dec 24 '13 at 01:15 AM

ShawnFeatherly gravatar image

ShawnFeatherly
16 2

(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 ()
{
    if(Input.GetKeyDown(KeyCode.R))
    {
       gameObject.renderer.material.color = Color.red;
    }
    if(Input.GetKeyDown(KeyCode.B))
    {
       gameObject.renderer.material.color = Color.blue;
    }
    if (Input.GetKeyDown (KeyCode.T))
    {
       gameObject.renderer.material.color = Color.white;
    }
}
more ▼

answered Dec 24 '13 at 02:04 AM

GraviterX gravatar image

GraviterX
69 2 5 17

(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

By RSS:

Answers

Answers and Comments

Topics:

x774
x146

asked: Jan 28 '13 at 01:45 AM

Seen: 10192 times

Last Updated: Feb 01 at 05:50 AM