- Home /
Fading an Object's Opacity over time via script.
I am using
var colorStart = Color.red;
var colorEnd = Color.green; var duration = 1.0;
function Update () { var lerp = Mathf.PingPong (Time.time, duration) / duration; renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp); }
from the refrence which works great for changing the color. But how can I get it to also change the opacity of the object. I want to fade it away. How do I effect the alpha channel. I have been trying something like this
var colorAlpha = Color.a;
function Awake(){ colorAlpha = 0; }
but to no effect.
Answer by Jessy · Sep 11, 2010 at 04:52 PM
Most shaders do not alpha blend. Use a shader from the "Transparent" section.
Edit: The shader you're using doesn't work, because the alpha of the color is not preserved when using lighting. Fortunately, you don't need lighting at all. Here's a shader that will work:
Shader "Tinted Alpha Blend" {
Properties { _Color ("Tint (A = Opacity)", Color) = (1,1,1,1) _MainTex ("Texture (A = Transparency)", 2D) = "" }
SubShader { Tags {Queue = Transparent} ZWrite Off Blend SrcAlpha OneMinusSrcAlpha
Pass {
SetTexture[_MainTex] {
ConstantColor [_Color]
Combine texture * constant
}
}
}
}
this is the shader I am using. Because it needs to be self illu$$anonymous$$ated.
Shader "AlphaSelfIllum" { Properties { Color ("Color Tint", Color) = (1,1,1,1) $$anonymous$$ainTex ("SelfIllum Color (RGB) Alpha (A)", 2D) = "white" } Category { Lighting On ZWrite Off Cull Back Blend SrcAlpha One$$anonymous$$inusSrcAlpha Tags {Queue=Transparent} SubShader { $$anonymous$$aterial { Emission [Color] } Pass { SetTexture [$$anonymous$$ainTex] { Combine Texture Primary, Texture Primary } } } } }
It doesn't need to be "self-illu$$anonymous$$ated". It just doesn't need to take lighting into account.
This is exactly what I was looking for also! Awesome. :D
Thank you very much! I confirm that this works in Unity 4 too.
Answer by Julian-Glenn · Sep 11, 2010 at 04:45 PM
If the script is attached to the object you want to fade then this should work to access the alpha value
renderer.material.color.a = 0.5;
Perhaps this shader may work or at least some of the things discussed in the thread
I have tried that, but I am using a png that has its own alpha and this code does not seem to have an effect on it.
Answer by JoeStrout · Nov 28, 2013 at 08:47 AM
That may have worked in some version of Unity, but in 4.3, I get a compile-time error (Cannot modify a value type return value of `UnityEngine.Material.color').
Your answer
Follow this Question
Related Questions
Transparent Fade Depth 0 Answers
Fade in and fade out effect in game? 2 Answers
Gui appear and then fade to black 1 Answer
How do I Fade Terrain Tree's to Blend with Billboard? 1 Answer