- Home /
Duplicate Question
Changing material alpha under runtime
Hi!
I have looked around the web for almost 30 minutes now and I have tested my own ideas but nothing have worked what so ever. I am trying to make the alpha fade out when the object is invisible and fade in when it is visible.
This is my code:
void Update () {
if(renderer.isVisible) {
playerNoise.renderer.material.color.a = 0.5f;
}
else {
playerNoise.renderer.material.color.a = 0.5f;
}
}
But I just get this error "Cannot modify a value type return value of `UnityEngine.Material.color'. Consider storing the value in a temporary variable"
I don't know what to do. If you know how I can do this, please tell me!
Thanks
You've been looking for 30 $$anonymous$$utes?! Typing the error message into Google gave me the answer in the top hit.... http://answers.unity3d.com/questions/60453/fading-out-using-rendermaterialcolora-doesnt-work.html
Answer by pstreef · Jun 16, 2014 at 08:29 PM
You cannot modify this structure this way. You can however create a whole new color from the old color with a different Alpha:
UnityEngine.Color oldColor = playerNoise.renderer.material.color;
playerNoise.renderer.material.color = new UnityEngine.Color(oldColor.r,oldColor.g,oldColor.b,0.5f);
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Fade out a material that has a texture 1 Answer
Correct Unlit Shader to use when Fading a Material 1 Answer
Renderer.material not cloning? 3 Answers
Numbers in a path? 1 Answer