Changing a material.color.a
I am trying to change a materials alpha on runtime.
I have googled and a lot of answers gave me the following code:
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Stuff : MonoBehaviour {
public Rigidbody Body { get; private set; }
void Awake () {
Body = GetComponent<Rigidbody>();
}
void Update () {
//answer I found, intend to change the 0.5f when it works.
Color c = GetComponent<MeshRenderer>().material.color.a = 0.5f;
}
}
However, I keep getting the following error: Cannot modify the return value of 'Material.color' because it is not a variable
Answer by Tymewiz · Mar 18, 2016 at 11:58 PM
Try storing the value for the color in a temporary variable like so :
Color tempcolor;
void Update () {
tempcolor = GetComponent<MeshRenderer>().material.color;
tempcolor.a = .5f;
GetComponent<MeshRenderer>().material.color = tempcolor;
}
The reason that
GetComponent<MeshRenderer>.color.a = .5f;
wont work is because the left hand side is a copy of the color's alpha component then i'm trying to overwrite it with .5f which is a useless operation.
also make sure the object's rendering mode is transparent.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need Help for constant alpha shader (example image) 0 Answers
Tint Multiple Textures Separately 1 Answer
How can I change the alpha value of multicolored text to 0? 1 Answer
While generating Face Objects - What shader to use to get them appear 0.5f alpha? 0 Answers
How to modify color.alpha in UI Image? 4 Answers