- Home /
Fade GameObject alpha with Standard shader
having a tough time trying to do something that should be simple.
How to simply fade a game object that has a Standard shader applied?
This is my code. If I switch the shader to Sprites.default it works.
public void foo(float val)
{
_isFade=true;
float time=2.0f;
float alphaVal=0.0f;
LeanTween.alpha(gameObject, alphaVal, time);
Debug.Log("YES calling fade in foo !!!! and gameObject = "+gameObject);
}
Answer by TrainWrek · Jun 23, 2015 at 10:58 PM
In the standard shader change rendering mode to "Fade" and the following C# script is an example of fading over time.
public class FadeAlpha : MonoBehaviour {
[SerializeField] private float fadePerSecond = 2.5f;
private void Update() {
var material = GetComponent<Renderer>().material;
var color = material.color;
material.color = new Color(color.r, color.g, color.b, color.a - (fadePerSecond * Time.deltaTime));
}
}
you just saved me another several hours of googling thanks!
Hey TrainWrek,
I've tried applying this script to a skeletal mesh object that I have in my scene and I get the error "$$anonymous$$issingComponentException: There is no 'Renderer' attached to the "Sign_Turn 01 Left" game object, but a script is trying to access it."
So I added a skinned mesh renderer to the object (don't know why a skeletal mesh wouldn't have a renderer already assigned to it when I dragged it in from the project window) but it still doesn't work.
Any idea how to solve this?
I have an asset that I purchased, only the child components have a renderer attached. Every part of the character has a renderer with a texture.
So, you could go for all children of the root transform (parent) and for each object when renderer is not null apply the fade effect.
This other code may help:
http://answers.unity3d.com/questions/225438/slowly-fades-from-opaque-to-alpha.html
See the IEnumerator FadeTo.
All this function does for me is hide the object. no fade.
The function in the post below did the job for me, but you'll have to update a couple of the lines in order for it to work. I've got a reply on that post detailing which ones.
This worked great for me. $$anonymous$$ake sure the shader is set to fade. I referenced https://answers.unity.com/questions/1004666/change-material-rendering-mode-in-runtime.html to change from the opaque to fade during runtime so I didn't need two materials.
Answer by Foysal-Ahmed-Emon · Jun 21, 2016 at 05:03 AM
Set the material : Standard > Fade
Then use this FadeOut3d Function :
public static IEnumerator FadeOut3D (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);
}
}
Then call the FadeOut3d Function form Start :
using UnityEngine;
using System.Collections;
public class VanishObject : MonoBehaviour
{
void Start ()
{
StartCoroutine (transform.FadeOut3D (0, true, 10));
}
}
The fadeout3d function was write from unity3d default function fadeout which work with SpriteRenderer. But for 3d Object we need Only "Renderer ".
Thanks
I have a beginner's question: Q: where do I define the FadeOut3D() function?
I'm trying to define it in the same class as the start() that uses it (sbinetFade), but then I get the following error: Assets/sbinetFade.cs(18,31): error CS1106: `sbinetFade.FadeOut3D(this UnityEngine.Transform, float, bool, float)': Extension methods must be defined in a non-generic static class
Update the StartCoroutine line to: StartCoroutine (FadeOut3D (this.transform, 0, true, 10));
And update the parameters list for the FadeOut3D function to: public static IEnumerator FadeOut3D (Transform t, float targetAlpha, bool isVanish, float duration)
All this does for me is to make the object unactive
Answer by richardzzzarnold · Sep 23, 2016 at 08:45 AM
Foysal Ahmed Emon I am getting the following error with this :
(this UnityEngine.Transform, float, bool, float)': Extension methods must be defined in a non-generic static class
What he wrote is an extension method. You need to put that in a new class that's declared as static. The name of the class is entirely up to you (you could call it FadeUtility, for example). Also, you should've wrote your post as a comment to his answer ins$$anonymous$$d of posting it as an answer.
Answer by jeremypatrickdahan · May 20 at 01:46 PM
A more "no code" option would be : Make the material rendering option "Fade". Create a script "ExposeAlpha" and attach it to your material, with the following code : using System.Collections; using System.Collections.Generic; using UnityEngine;
public class fade4 : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField] public float alpha = 1.0f;
private Renderer r;// = GetComponent<Renderer>();
void Start()
{
r = GetComponent<Renderer>();
}
// Update is called once per frame
void Update()
{
var material = r.material;
var color = material.color;
material.color = new Color(color.r, color.g, color.b, alpha);
}
}
Now you have an alpha parameter exposed, that you can use in animations.
Your answer

Follow this Question
Related Questions
Unity 4: Image Effect "Bloom" ignoring alpha channel mask 0 Answers
How do you make an X-ray vision following the mouse? 1 Answer
Can't change RGBA shader values in animation clip when I set them to legacy? 0 Answers
Fading between two objects in UnityScript 0 Answers
transparent RGBA PNG textures worked in 4.6, how can I get them working in Unity5? 1 Answer