Fading out using render.material.color.a +=/-= doesn't work

Hi,

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;
}

}

.

renderer.material.SetAlpha(renderer.material.color.a - .1F);

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. ,

In Unity 5:

Color color = GetComponent<Renderer>().material.color;
color.a -= 0.1f;

GetComponent<Renderer>().material.SetColor("_Color", color);

If you’re using Standard Shader, make sure the Rendering Mode is set to transparent.

public static IEnumerator FadeOutThreeD (this Transform t, float targetAlpha, bool isVanish, float duration)
{
Renderer sr = t.GetComponent ();
float diffAlpha = (targetAlpha - sr.material.color.a);

		float counter = 0;
		while (counter < duration) {
			float alphaAmount = sr.material.color.a + (Time.deltaTime * diffAlpha) / duration;
			sr.material.color = new Color (sr.material.color.r, sr.material.color.g, sr.material.color.b, alphaAmount);

			counter += Time.deltaTime;
			yield return null;
		}
		sr.material.color = new Color (sr.material.color.r, sr.material.color.g, sr.material.color.b, targetAlpha);
		if (isVanish) {
			sr.transform.gameObject.SetActive (false);
		}
	}

using UnityEngine;
using System.Collections;

public class VanishObject : MonoBehaviour
{

	void Start ()
	{
		
		StartCoroutine (transform.FadeOutThreeD (0, true, 10));
	}

}

try this

color.a = new Color(color.a - 0.1, color.r, color.g, color.b);