- Home /
How to fade a game object will all its children?
I'm trying to use a fade effect for my menus and I want the whole menu to fade out, the menus is in a container. I tried iterating through the children of the container and changing the alpha value of the materials, but then as it fades you can see an overlap effect when there is more than one object on top of another.
Is there a way to fade a game object and its children together so I don't get the additive effect when there is more than one object?
Answer by Kiloblargh · Nov 26, 2013 at 08:54 PM
You just need a different shader, specifically one that writes to the depth buffer. Here is an example of that:
Thanks that works great, never really messed around with shaders :D
Has anyone gotten the shader linked (Specular VertexLit w Z) to work in the Animator/Animation system? I can fade it with a script, and even though it fades in the Inspector, it doesn't fade in the Scene or Game views.
Answer by Ill-Fish · Aug 26, 2015 at 08:17 AM
with the new UI of Unity simply add a Canvas Group to the container and fade out the alpha of the Canvas Group. The children will also fade out with the parent object.
This should be the accepted answer – it's the easiest method by far.
Agreed, this is the most straight-forward "Unity" way to accomplish this.
Answer by deltamish · Nov 26, 2013 at 06:41 PM
hi,
well there is pretty simple solution
var FadeSpeed:flaot = 2;
var Fade:boolean = false;
function FadeAll(){
for(var gb:GameObject in gameObject){
gb.renderer.material.color = Mathf.Lerp(1,0,Time.deltaTime * FadeSpeed);
}
//Just in cae if the parent alpha hasnt changed
gameObject.renderer.material.color = Mathf.Lerp(1,0,Time.deltaTime * FadeSpeed);
}
function Update(){
if(Fade)
{
FadeAll();
}
}
Atttach this script to the parnet object and at runtime when you set Fade to true it will automaticall fade
that doesn't seem to work for me. I've amended the script:
for (var _renderer : Renderer in gameObject.GetComponentsInChildren(Renderer) ) {
if (_renderer) {
for (var _material : $$anonymous$$aterial in _renderer.materials) {
var bC = _material.color;
_material.color = Color(bC.r, bC.g, bC.b, $$anonymous$$athf.Lerp (bC.a, 0.0, (Time.deltaTime * FadeSpeed)));
}
}
}
still no joy...
Your answer
![](https://koobas.hobune.stream/wayback/20220613122830im_/https://answers.unity.com/themes/thub/images/avi.jpg)