You are here
Home > Programming > Change Cube color in code in JavaScript

Change Cube color in code in JavaScript

javaScript Nick

Change cube color in Javascript C+++

Changing the color of a gameObject in JavaScript

Very easy to follow in Unity and most other JavaScript editors.

  • Open up your unity and create a 3D cube.
  • Create a .js Script file, name it cubeColor.js
  • Attach the cubeColor.js file to the gameObject you want to change color of.

Now, open the cubeColor.js file in monodevelop script editor or your favorite editor and add below this code in the function Start.

function Start () {

transform.GetComponent.<Renderer>().material.color =;


Click save and go back to Unity. Your cube will now be green on play.

That’s it! – want more? keep reading.

You can change to many other colors just by changing green to red or blue and so on.

  • color =;
  • color =;
  • color =;

transform.GetComponent.<Renderer>().material.color =;

extra: in C# remove the dot ( . ) before the <Renderer>, now it works in your C# project.

Try it now.
If you have any problems then please leave a comment below and I or someone will help you out.

The Game Never Ends


For more in-depth color change, You can change the R,G,B,A values.



gameObject.GetComponent.<Renderer>().material.color = Color32 (0,0,0,255);

Each value has a range from 0-255 that makes all the colors. How Cool is that!.

If you liked this and/or it has helped you, please leave a comment below. This way you help us get better.

Developer & Publisher.

Leave a Reply


By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.