I have a scene in my game where the player has to pick up objects with the mouse cursor. I can shoot rays, and find out what object I’m touching, but I can’t seem to change the color of the object.
Here’s some code:
var colorRed = Color.red;
var redColorValue : float;
var randomObject;
randomObject = GameObject.Find("MonkeyStatue");
redColorValue = 0.5; randomObject.renderer.materials[0].mainTexture.color = (colorRed*redColorValue);
I would expect to change the Monkey Statue’s red color value, but instead I get an error:
“MissingFieldException: Field’UnityEngine.Texture2D.color’ not found”
What? I’ve done this before with other objects and it worked. I guess the difference here is that I’m using a transparent shader for the material instead of a standard diffuse.
Any tips?
Texture has no color porperty, but material does:
randomObject.renderer.material.color = (colorRed*redColorValue);
Do’h, my mistake. Thanks for the reminder.
While we’re on the subject, is there anyway to effect the opacity of a meshes mainTexture?
Sure, just modify the alpha value of the material color:
randomObject.renderer.material.color.a = 0.5;
This doesn’t work with all shader, though (before you ask: I don’t know much if anything at all about ShaderLab).
Ah thanks.
I’m using a transparent shader, so it may not work, I’ll test it.
If it doesn’t, I’ll just do a texture swap, and make a texture with the correct opacity on my end (photoshop). Just food for thought if anyone else encounters this type of task later.
I think that If your hardware target has vertex/pixel shades then it is definitely worth looking into. I wish that You will have to use colour_op_ex, Whenever You will blind the colour. I hope that It could be helpful.