Anyone know how I can make a gameobject fade out smoothly? (C# script) (64922)

I’ve tried this but I don’t know what to do after writing the actual code and assigning it to the gameobject I want to fade out.

using UnityEngine;
using System.Collections;

public class fader : MonoBehaviour {

private Color thisColor;
public bool destroyOnFaded=true;
private bool isFading = false;
private Color col = new Color();

// Use this for initialization
void Start () {
	thisColor = renderer.material.GetColor("_Color");
	col.r = thisColor.r;
	col.g = thisColor.g;
	col.b = thisColor.b;
	col.a = thisColor.a;
	isFading = true;
}

// Update is called once per frame
void Update () {
	if(isFading){
	col.a=-0.99f;//-= 0.5f * (float)Time.deltaTime;
	renderer.material.SetColor("_Color", col);		
	}
}

}

You have an error where you're setting the alpha: col.a = -0.99f; So you're setting alpha to -1 every frame, which isn't correct. The code you have commented out looks much better and might actually work.

We'll also need to know what shader you're using. Not all shaders have have transparency.

2 Answers

2

Use iTween’s FadeTo.

iTween

And make sure the shader you are using have transparency.

Ok, thanks for the help guys.