UnityGameProjectsCode / Rise2Point0Game / Obstacles / TimedObstacle.cs
TimedObstacle.cs
Raw
using UnityEngine;

public class TimedObstacle : MonoBehaviour
{
    //given a wait time, switch between active and disabled states
    //by changing the color and opacity of the glow and object

    public float waitTime;
    public SpriteRenderer objectSprite;
    public SpriteRenderer glowSprite;
    public Color activeObjectColor;
    public Color fadedObjectColor;
    public Color activeGlowColor;
    public Color fadedGlowColor;
    private float timeSinceChange;
    private BoxCollider2D coll;
    private AudioSource aSource;

    private void Start()
    {
        coll = GetComponent<BoxCollider2D>();
        aSource = GetComponent<AudioSource>();
    }

    private void Update()
    {
        timeSinceChange += Time.deltaTime;

        if (timeSinceChange >= waitTime)
        {
            if (coll.enabled)
            {
                objectSprite.color = fadedObjectColor;
                glowSprite.color = fadedGlowColor;
                coll.enabled = false;
                aSource.Stop();
            }
            else
            {
                objectSprite.color = activeObjectColor;
                glowSprite.color = activeGlowColor;
                coll.enabled = true;
                aSource.Play();
            }

            timeSinceChange = 0.0f;
        }
    }
}