- Home /
programmatically create a texture or change a texture's color?
I have some cubes that I would like to give a color. Ideally I want to create it programmatically, so i don't need to make custom textures. I was wondering how to do this and then how to change the color? It will be a one color texture.
If it's not possible to do this with code only: how do I give a regular white or black texture a color?
Answer by ramonfr · Dec 25, 2011 at 08:22 PM
You could simply change the main colour of the material.
MyObject.renderer.material.color = Vector3(255,0,0);
// sets the color to red
Answer by gegc · Dec 25, 2011 at 10:14 PM
Changing just color is easy. Say you have a GameObject o
with a MeshRenderer component. Then you just do
o.GetComponent<MeshRenderer>().material.color = somecolor;
You can also do
o.GetComponent<MeshRenderer>().materials[X].color = somecolor;
to change colors on specific materials if you have multiple materials in your model (having X=0 is equivalent to just saying material.color
).
Setting textures should work the same way except with some combination of material.mainTexture and material.SetTexture(). I haven't messed with either of them yet so I don't know exactly how they work.
Hope this helps!
Answer by Paulius-Liekis · Dec 25, 2011 at 06:05 PM
Why can't you just change diffuse color on a material? That would change color of a cube. There is no need to create textures for that.