Fade Object and Children

Hi Guys, I’m new to Unity and I’m trying to work with Materials and alpha properties, but I’ve got a problem already…

I need to fade out an object that has several children (and some of those children also have sub children). When I Change the alpha of material of the main object in the editor, all sub objects (children) also get that new alpha value. The problem is that in runtime, using c#, I can’t make fadeout my object… I figured out that I needed to modify the alpha of all children so I did:

void changeAlpha(GameObject targetObj, newAlpha){
    MeshRenderer[] all = targetObj.GetComponentsInChildren<MeshRenderer>();
    for(int i=0; i<all.Length; i++){
        Material cMat = all[i].renderer.material;                // Get currentMaterial
	all[i].renderer.material.color = new Color(cMat.color.r, cMat.color.g, cMat.color.b, newAlpha);
    }
}

It’s working fine (well, not completely… some objects for some reason still don’t get the alpha), the problem is that it consumes a hell of resources, actually it freezes my computer some times. Probably because I’m calling the method several times per second, when a slider change its value. And also, there are up to 3 different objects to which I need to fade out at the same time…

It’s there any better way to do this? How the editor does it??

Any help will be appreciated!

Alex.

using UnityEngine;
using System.Collections;

public class FadeTest : MonoBehaviour
{
	Color startColor;
	Color currentColor;
	Color endColor;
	bool shouldFade = false;
	float startTime;
	float endTime;
	public float seconds = 5.0f;
	float t;	

	// Use this for initialization
	void Start ()
	{
		startColor = gameObject.renderer.material.color;		
		endColor = new Color(startColor.r, startColor.g, startColor.b, 0.0f);		

		for ( int childIndex = 0; childIndex < gameObject.transform.GetChildCount(); childIndex++)
		{
			Transform child = gameObject.transform.GetChild(childIndex);			

			child.gameObject.AddComponent<FadeTest>();
		}
	}

	// Update is called once per frame
	void Update ()
	{
		if (Input.GetMouseButtonDown(0))
		{
			shouldFade = true;

			startTime = Time.time;

			endTime = startTime + seconds;
		}		

		Fade();
	}	

	void Fade()
	{
		if ( shouldFade )
		{
			t = Time.time / endTime;

			currentColor = Color.Lerp(startColor, endColor, t);			

			gameObject.renderer.material.SetColor("_Color", currentColor);			

			if ( currentColor == endColor )
			{
				shouldFade = false;
				startTime = 0.0f;
				endTime = 0.0f;
				t= 0.0f;
			}
		}
	}
}

only requirement is that the material is of the transparent breed.

In my game I’m using a code thats similar to dood’s. The only difference is that my Material uses a shader that has _AlphaMod to change Material alpha.

But I’m not sure this is the best approach. It’s to my knowledge that everytime you make any change to a material (by script) it get duplicated so the change won’t affect all objects with the same material.

Btw, if you want to change the alpha/color of all objects that uses the same material, use this:

gameObject.renderer.sharedMaterial.SetColor("_Color", currentColor);

You’re creating a new Color every frame for every object, of course that consumes lots of resources, try modifying the color instead.

Examples: http://forum.unity3d.com/threads/113048-GameObject.Find(-quot-this-*-quot-).renderer.material.color.a-0.1

kiberkiller, I think the bottle neck here is not a new color (since color is a struct) but the creation of a new material every frame.

I’m not proficient in C# code so I haven’t noticed that he’s creating a new material every time but if that’s the case then that’s definitely what’s causing the slowdown.

Well, I say that because of this:

Modifying material will change the material for this object only.
If the material is used by any other renderers, this will clone the shared material and start using it from now on.
source: Unity - Scripting API: Renderer.material

Wow Guys, Thanks sooo much for all your replies. I just woke up and i’m about to test dodo’s approach. But beneton’s method looks pretty straight forward. BTW, I can’t believe I didn’t notice I was creating a material every frame!!! suddenly all my resource consumption make sense! Thanks for pointing that out kiberkiller.

I’m gonna try both methods and I’ll let you know.

Cheers,

Alex.

Hi guys, I finally tested dodo’s method and worked like a charm!!

Thanks so much!!!

Thank you :slight_smile:
boi its working :slight_smile:

There are some bugs with “dood” code; line 49 is making the transition wrong, it should read:

t = (Time.time - startTime) / duration;

(plus you don’t need the endTime variable)