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

I’ve tried this but it wont 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);
}
}
}

Well in the example above, you’re just setting the alpha to -99% always, which wouldn’t do a fade.
Also, put your code in blocks, it makes it a lot easier to read.

The commented one seems to be more appropriate with the observation that you should set isFading to false after col.a reach zero.

OK, I wanted to test it myself and this is what i come up in 5 mins. You also need to assign a transparent shader to your object in order for this to function.

using UnityEngine;
using System.Collections;

public class fade : MonoBehaviour
{
    public int fadeSpeed = 3;
    private bool isDone = false;
    private Color matCol;
    private Color newColor;
    private float alfa = 0;

    // Use this for initialization
    void Start()
    {
        matCol = renderer.material.color;
    }

    // Update is called once per frame
    void Update()
    {
        if (!isDone)
        {
            alfa = renderer.material.color.a - Time.deltaTime/(fadeSpeed==0?1:fadeSpeed);
            newColor = new Color(matCol.r, matCol.g, matCol.b, alfa);
            renderer.material.SetColor("_Color", newColor);
            isDone = alfa <= 0 ? true : false;
        }
    }
}