Before updating to Windows 10 you may need to return your Pro license. More information here

Changing material Color using RBG?

Hello, Just to Be clear i am using JavaScript I am wondering if it is possible to change the color of a material using RBG values? I would like to make one GUI slider that accesses the numbers from 0-255 on R, G, B and alpha value. (That GUI slider would access all the possible colors from most right to most left.

I found this snippet of code to change the color of the material in RBG:

 material.color = newColor(255f, 255f, 255f, 1);

Am I on the right track?

Thank You


more ▼

asked Jul 26, 2013 at 10:20 PM

avatar image

Daniel G
500 70 748 92

I would also like to be a will to change the shininess of the objects material

Jul 26, 2013 at 10:24 PM Daniel G
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

If you want to use 0-255 integer values, use Color32.

 material.color = Color32(255, 255, 255, 1);
more ▼

answered Jul 26, 2013 at 10:51 PM

avatar image

121k 80 365 774

This is good to know! Thanks for showing this. I'm sure it'll be useful.

Jul 26, 2013 at 10:58 PM LemonLake

Thank you! This is the answer I'm looking for, simple, straightforward!

For any one that wants to read more about Color32 http://docs.unity3d.com/Documentation/ScriptReference/Color32.html

Jul 26, 2013 at 11:09 PM Daniel G
(comments are locked)
10|3000 characters needed characters left

newColor, as far as I know, does not exist. Here's a function to create a color for what you want:

    function getColor(red:int, green:int, blue:int, alpha:int):Color{
        return Color((1/255)*red, (1/255)*green, (1/255)*blue, (1/255)*alpha);

(note: not tested, but should work :))

To use it, simply do:

 material.color = getColor(255,255,255,255);

Red, green, blue and alpha are numbers between 0 and 255.

UPDATE: As for shininess, that is up to your shader. See http://docs.unity3d.com/Documentation/ScriptReference/Material.SetFloat.html

more ▼

answered Jul 26, 2013 at 10:36 PM

avatar image

201 1 5 5

Looks like a typo, no space between the keyword new and Color. Just as a note, the keyword new and denotation f doesn't have to be included in uJS (though I like to use them!). uJS has a lot of implicit typecasting.

 material.color = new Color( 0.1f, 0.1f, 0.1f, 1 );
 // is the same as below in uJS
 material.color = Color( 0.1, 0.1, 0.1, 1 );

Nice function =]

Jul 26, 2013 at 10:40 PM alucardj

@alucardj Thanks for telling me, more into C# now, I last used javascript properly a few years ago. Fixed.

Jul 26, 2013 at 10:41 PM LemonLake

"f" doesn't have to be included in Unityscript because numbers with decimals are floats by default; if you want doubles then you need to specify "d". C# is the opposite, where numbers with decimals are doubles by default. You can specify "d" but it's not necessary, whereas you need to specify "f" if you want floats.

Jul 26, 2013 at 10:54 PM Eric5h5

That function isn't necessary, since Color32 exists. Also it wouldn't work since you're doing integer division.

Jul 26, 2013 at 10:55 PM Eric5h5

Excellent function however I'm leaning toward color32, because i can replace the numbers easily with floats and don't have to have integer division! BUT thank you for the shininess! How do i accept more than one answer it wont let me? I have seen it done before though!

Jul 26, 2013 at 11:07 PM Daniel G
(comments are locked)
10|3000 characters needed characters left


Each color component is a floating point value with a range from 0 to 1.

Try :

 var r : float = value / 255.0f;

Shininess would be relative to the Shader.

more ▼

answered Jul 26, 2013 at 10:29 PM

avatar image

31.2k 172 1073 243

But looks like color 32 is already 255? SO this would be only necessary if i was using the function lemon lake submitted correct?

Jul 26, 2013 at 11:08 PM Daniel G
(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



Answers and Comments



asked: Jul 26, 2013 at 10:20 PM

Seen: 3026 times

Last Updated: Jul 26, 2013 at 11:12 PM