﻿//Author - Devin Reimer - http://blog.almostlogical.com
using UnityEngine;
using System.Collections;

public class RemoveAfterTime : MonoBehaviour
{
    private float destroyDelay = 1.4f;
    private float physicsEndDelay = 1f;
    private float time;
    public Material newMaterial;
    public void Start()
    {
        time = Time.time;
    }

    public void Update()
    {
        if (Time.time > time +destroyDelay)
        {
            renderer.material = newMaterial;
            Color color = renderer.material.color;
            float newAlpha = 1-(Time.time - (time + destroyDelay))*2;
            if (newAlpha<0)
            {
                Destroy(gameObject);
            }
            else
            {
                color.a = newAlpha;
                renderer.material.color = color;
            }
        }
        else if (rigidbody != null && Time.time>time+physicsEndDelay)
        {
            Destroy(rigidbody);
        }
    }

}