I'm trying to fade out my object by reducing its alpha but it seems reluctent to accept this code;
renderer.material.color.a -= 0.1f;
The error message I get says the following;
"Error: Cannot modify the return value of 'UnityEngine.Material.color' because it is not a variable"
I do not see how this is not a variable I can modify. I vaguely remember using this code before though now I seem to be missing something.
EDIT IN RESPONSE TO ANSWER;
using UnityEngine;
using System.Collections;
public class MyRenderer : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonUp(0))
{
Color color = renderer.material.color;
color.a -= 0.1f;
renderer.material.color = color;
}
}
}
This doesn't do anything when I attach it to a gameobject. Created a new gameobject, added the script, ran it, did nothing :( I'm using the latest version of unity btw. Thing is I didn't have this problem when using 2.6
Are you using an old version of Unity? The current version should tell you
Cannot modify a value type return
value of `UnityEngine.Material.color'.
Consider storing the value in a
temporary variable
You just do what it says:
Color color = renderer.material.color;
color.a -= 0.1f;
renderer.material.color = color;
Adjusting just the alpha of the main color is a pretty common operation, so you may want to use this extension method until a better option is available. An extension property would be better, but those aren't in C# yet.
using UnityEngine;
public static class ExtensionMethods {
public static void SetAlpha (this Material material, float value) {
Color color = material.color;
color.a = value;
material.color = color;
}
}
This post is pretty old, but for those of you who might come across this problem.
Make sure that your material shader is set to Transparent/Diffuse or some form of transparent material.
Otherwise changing the alpha component does nothing.
An alternative to decrementing the alpha component :
renderer.material.color -= new Color(0,0,0,.10f);
Though not super efficient its gets the job done. ,