-2

fade in fade out of unity3 in prefeb.I am using prefeb which consist two primitive game object cube and capsule, and I want to prefeb will be fade in fade out with time duration using C# script of unity3d.

Ashok
  • 5
  • 4

1 Answers1

0

First create a new material, apply that to the prefabs you would want to fade in out out.

Then throw a script on your prefab that looks something like this.

public class FadeScript : MonoBehaviour
{
    public Material fadeMaterial;
    public float fadeTime = 2f;
    private float fadeTimer = 0f;

    // Start is called before the first frame update
    void Start()
    {
        fadeTimer = 0f;
    }

    // Update is called once per frame
    void Update()
    {
        if (fadeTimer < fadeTime) fadeTimer += Time.deltaTime;

        fadeMaterial.color = new Color(
            fadeMaterial.color.r,
            fadeMaterial.color.g,
            fadeMaterial.color.b,
            fadeTimer / fadeTime);
    }
}

You can add a flag on it to start the fade, and mimic this code to accomplish the fade out. Make sure to attach your material onto the script.

Branden
  • 347
  • 1
  • 14