Hi there.
I am in the process of making certain game objects transparent if they are between the player and the camera. When they leave this area they return to the normal opacity (1.0f).
I am aware that a new copy of the material is created when changing the color alpha, but I don’t know how to proceed to set the original material back in order to get the object batched.
Here is the code I am using:
private static Material defaultMaterial;
// Use this for initialization
void Start()
{
if (defaultMaterial != null)
{
defaultMaterial = (Material)Material.Instantiate(this.renderer.material);
}
}
// Update is called once per frame
void Update()
{
if (timeToFade > 0)
{
timeToFade -= Time.deltaTime;
if (shouldFadeIn)
{
Color c = this.renderer.material.color;
c.a = minimumFade + ((fadeDuration - timeToFade) * (1.0f - minimumFade) / fadeDuration);
this.renderer.material.color = c;
if (timeToFade <= 0)
{
this.renderer.material = defaultMaterial;
Debug.Log("Faded IN");
}
}
else
{
Color c = this.renderer.material.color;
c.a = minimumFade + (timeToFade * (1.0f - minimumFade) / fadeDuration);
this.renderer.material.color = c;
if (timeToFade <= 0)
{
Debug.Log("Faded OUT");
}
}
}
}
The approach in this code doesn’t work. Any ideas on how to do it?