using UnityEngine;
using System.Collections;

public class DestroyablePlatform : MonoBehaviour
{
    public Renderer renderTarget;
    public Material material;
    public float delay;
    public Color deathColor;

    private bool _triggered;

    private void Start()
    {
        renderTarget.material = material;
        _triggered = false;
    }

    public void TriggerDestruction()
    {
        if (!_triggered)
        {
            _triggered = true;
            StartCoroutine(BeginDestruction());
        }
    }

    private IEnumerator BeginDestruction()
    {
        float elapsedTime = 0f;
        Color startColor = renderTarget.material.color;

        while (elapsedTime < delay)
        {
            yield return null;
            elapsedTime += Time.deltaTime;
            renderTarget.material.color = Color.Lerp(startColor, deathColor, elapsedTime / delay);
        }

        Destroy(gameObject);
    }
}
