- Home /
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
It's working for me... $$anonymous$$aybe post some more script you're using?
AngryOld$$anonymous$$an, that's irrelevant, all that matters is that the shader you're using has a property called _Color, and that the alpha channel of is mapped to some effect.
The problem is that Color
is a value type. $$anonymous$$aterial.color
is not a variable but a property (with get and set method). By querying .color
the get method returns a copy of the Color struct. The += operator just works on the struct member .a
of the temporarily returned copy. The value get changed, but the change happens on the copy. To change the actual property you have to assign a Color struct to .color
in order to invoke the setter. As Jessy said it will work in JS but in C# it will only work if you do it with a temp-variable. $$anonymous$$ost C# examples do it the right way.
And what effect do you expect from a opaque diffuse shader when you tweak the inco$$anonymous$$g alpha value? In order to have a transparent effect you need to use a transparent shader that uses the alpha value to perform alpha blending.
Answer by Jessy · Apr 24, 2011 at 09:56 PM
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);
not working for me, I can see the value change in the inspector but nothing appears to be happening on screen, check the code i'm using, I edited my question
If there aren't any errors, then you're just not using a shader that responds to changes in _Color's alpha value. Are you seeing the little white bar change in the material panel? Your code works fine for me.
just wanted to post an update: renderer is now deprecated http://docs.unity3d.com/ScriptReference/GameObject-renderer.html
Answer by joe_bar · May 30, 2012 at 09:45 PM
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. ,
Thanks a bunch for this tip. I thought it was something like this but really didn't have enough experience to "know" what the real problem was.
What material can be completely opaque and completely transparent?
You just need to set the rendering mode to transparent
Answer by jmasinteATI · Aug 13, 2015 at 08:10 PM
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.
http://docs.unity3d.com/ScriptReference/Material.SetColor.html
how can i set rendering mode ? I'm beginer and can work with sprites? please
In the material component in the Inspector, beneath the name of the shader you're using, you'll see "Rendering mode" with a drop down box that displays the Rendering $$anonymous$$ode. By default, it's set to "Opaque." This is for the standard shader. Not all shaders have this drop down box.
Here is a screenshot: http://docs.unity3d.com/$$anonymous$$anual/StandardShader$$anonymous$$aterialParameterRendering$$anonymous$$ode.html
Answer by Kieran Chandler · Jun 17, 2012 at 07:43 PM
try this
color.a = new Color(color.a - 0.1, color.r, color.g, color.b);
Answer by Foysal-Ahmed-Emon · Jun 21, 2016 at 04:55 AM
public static IEnumerator FadeOutThreeD (this Transform t, float targetAlpha, bool isVanish, float duration)
{
Renderer sr = t.GetComponent<Renderer> ();
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));
}
}