How to change material color of an Object

Guys, how may i change the tint color of a material in a existent GameObject in JavaScript?

6 Answers

6

gameObject.renderer.material.color = Color.white;

You have to either define a color, or you can use Color.white ('white' can be any of a number of colors, including white, red, yellow, black, gray, blue, cyan, green, etc.)

Or build your own Color using Color(R, G, B). R, G, & B in this function are floats from 0.0 to 1.0.

This works best if the base color of the material is 50% gray, that way you have room to move away from that color in any direction.

The actual effect of this is that you are adjusting the 'Main Color' of the material that is exposed in the Inspector at the top of the material.

Thanks :D

You do not have to do it with code if you want to make it a bit easier. Just left click in your preferred folder in this case Assets and then create–>Material. From there you can change in to a sprite if you want a solid color and you can experiment with them. Once you have the proper color just drag that material into the object.

allo!

When i use

gameObject.renderer.material.color = Color(0.777, 08, 0.604);

the color is not the same as when I use this color in the object.

if anyone knows why, thank you

Hi! I know this is an old answer but...: for some reason in code the RGB's 255 = 1, so if you wanted something white (and doing it with the RGB) it'd be: gameObject.renderer.material.color = new Color(1,1,1); So, you can do a simple conversion function: private float coloNumberConversion(float num) { return (num / 255.0f); } Now you can put the actual RGB you see in the inspector: for example, for a darkish brown Color color = Color.black; color.r = coloNumberConversion(94); color.g = coloNumberConversion(69); color.b = coloNumberConversion(31);

I know I am 8 years late but you can use Color32 you give it r g b a from 0 to 255 for example: new Color32(255,0,0,255) is red

In Unity 2019+, C#:

gameObject.GetComponent<Renderer>().material.color = Color.white;

http://solution2tech.blogspot.in/2013/10/change-material-color-in-unity-3d.html

If you need more complex recoloring (keeping some colors intact while changing other etc.), you can check Colorify on asset store

Hi guys! Thank you very much for the support! I have already resolved this issue. Once again thank you very much! All the best!