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 = Color.green;

}

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.green;
  • color = Color.red;
  • color = Color.blue;

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

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.

RGB

 

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.

Share

Leave a Reply