Home > Software engineering >  Is it possible to add custom RGB color when applying a color to a to an object via script in Unity?
Is it possible to add custom RGB color when applying a color to a to an object via script in Unity?

Time:07-30

I have this Script that I made to change the color to green when the pressure plate is triggered, So I am wondering if it's possible to use a custom RGB color in script instead of using a already set color like red, green, or blue.

here's the code I used to change the color:

PressurePlate.material.color = Color.green;

CodePudding user response:

If PressurePlate is of type MeshRenderer

PressurePlate.material.color = Color.green;

If PressurePlate is of type Renderer

PressurePlate.material.SetColor("_Color", Color.green);

Color green in RGB is rgb(0, 255, 0). If you want to set the opacity, then the color code is written as rgba(0, 255, 0, 0.6) (opacity is 0.6 in range 0 to 1 and a in rgba is alpha value). In unity, the Color takes in float values ranging from 0 to 1. If you have a color code rgb(255, 255, 0), in unity, it's new Color(255/255f, 255/255f, 0/255f). You divide each color code with 255.

Assuming PressurePlate is of type MeshRenderer,

Color someColor = new Color(0f, 1f, 0f, 1.0f);
PressurePlate.material.color = someColor;
  • Related