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, 2013 at 01:45 AM

avatar image

jjbloon
46 1 2 5

(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

avatar image

whydoidoit
55.5k 62 128 255

please help me how to change its background not color. Thanks

Feb 14 at 11:33 AM aSaDaliii

For the code example use the appropriate Component of a GameObject instead, it is gameObject.GetComponent().material.color = new Color(0.5f,1,1);

Mar 05 at 04:14 PM RoiDanton
(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, 2013 at 01:15 AM

avatar image

ShawnFeatherly
266 8 9

please help me how to change its background not color. Thanks

Feb 14 at 11:33 AM aSaDaliii
(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, 2013 at 02:04 AM

avatar image

GraviterX
238 16 916 73

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

x1080
x226

asked: Jan 28, 2013 at 01:45 AM

Seen: 40464 times

Last Updated: Mar 05 at 04:14 PM