- Home /
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?
Answer by Tasarran · Jan 24, 2012 at 09:54 PM
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.
seems this is deprecated you can update this to:
gameObject.GetComponent<Renderer> ().material.color = Color.green;
$$anonymous$$A$$anonymous$$E SURE NOT TO USE 0 TO 255! That's what I did.
Answer by Azbo · Nov 02, 2015 at 01:56 PM
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.
Answer by ruileitao · Sep 23, 2013 at 10:02 PM
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);
To note, the multiplication in your conversion is redundant. You'll see identical results by using:
return num / 255.0f;
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
Answer by CatsPawGames · Sep 15, 2014 at 09:08 PM
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!
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
How can i get mixed texture from one shader and set it for another object with another shader? 0 Answers
Laser Shader/Material? 0 Answers
UI mask with shader 5 Answers
Shader with multiple alpha textures, single color for each texture, how to? 1 Answer