Hey Developers!
The code I will post below is working perfectly fine, but I was just wondering if there was an alternative way of achieving this? To make it easier to understand, this is what the code is doing:
- Check if Fan Status is Set To False
- Start Fan Start Countdown
- Switch Fan On (Change Sprite Color)
- Switch Fan Off (Change Sprite Color x2)
- Repeat…
Thank you all for viewing or contributing! 
using UnityEngine;
using System.Collections;
public class PlatformFan : MonoBehaviour
{
// Variable Determine If Fan is On/Off
public bool FanActivated = false; // Set to False By Default
// Variable Determine The Fan On Duration
public float FanOnTimer = 2.0f;
public float FanCountdownTimer;
// Variable Stores The Fan Sprite Renderer
private SpriteRenderer FanSpriteRenderer;
// Variable Stores The Fan On/Off State
public Color RedColor; // Switched On
public Color AmberColor; // About To Go Off
public Color GreenColor; // Switched Off
// Use this for initialization
void Start ()
{
// Get Sprite Renderer Component
FanSpriteRenderer = gameObject.GetComponent<SpriteRenderer>();
// Create a Copy Of Fan On Timer
FanCountdownTimer = FanOnTimer;
}
// Update is called once per frame
void Update ()
{
// Check If Fan is Activated
if(FanActivated == false)
{
if(FanCountdownTimer >= 0)
{
Debug.Log("Fan Countdown Started!");
// Start Countdown
FanCountdownTimer -= Time.deltaTime;
}
else
{
// Execute Method To Switch Fan On
SwitchFanOn();
}
}
}
// Method To Switch Fan On
void SwitchFanOn()
{
Debug.Log("Fan Switched On!");
// Set Fan Status To True
FanActivated = true;
// Set Fan Color To Green
FanSpriteRenderer.color = GreenColor;
// Execute Method to Switch Fan Off
StartCoroutine(SwitchFanOff());
}
// Method To Switch Fan Off
IEnumerator SwitchFanOff()
{
// Divide Fan Timer So We Could Change The Color Twice
float CurrentFanTimer = FanOnTimer/2;
// Wait Amount Of Time Before Proceed Ahead
yield return new WaitForSeconds(CurrentFanTimer);
// Set Fan Color To Amber
FanSpriteRenderer.color = AmberColor;
// Wait Amount Of Time Before Proceed Ahead
yield return new WaitForSeconds(CurrentFanTimer);
// Set Fan Color To Red
FanSpriteRenderer.color = RedColor;
// Set Fan Status To False
FanActivated = false;
// Reset Countdown Timer
FanCountdownTimer = FanOnTimer;
Debug.Log("Fan Switched Off!");
}
}
In Programing there are a lot of ways to do something and often time you might find that there is something already available for you to use if you happen to know about it.
For what you are doing it looks like Invoke() might be your friend. In the interests of simplicity and for the next person looking to do something similar I wrote and tested something that does what looks like you are a trying to achieve.
Since yo are trying to time everything I recommend just a sequence of Methods times to call one after another completing an infinite loop. In the below example, Start() begins the process of turning a cube green, then in 2 seconds it will call a method to turn the cube yellow, then in another 2 seconds the cube will be turned red. And then the cycle repeats. This could get you all of the timings you need without all the extra code you put into your version.
You should only need about 1/3rd of the code you developed for this solution. And you should only need the Material or in your case SpriteRenderer components inside of the class, you pretty much do not need any of the other variables you created in the class.
using UnityEngine;
using System.Collections;
public class ColorMyCube : MonoBehaviour
{
Material CubeColor;
// Use this for initialization
void Start ()
{
CubeColor = this.GetComponent<MeshRenderer> ().material;
Invoke ("MakeGreen", 2f);
}
void MakeGreen ()
{
CubeColor.color = Color.green;
Invoke("MakeYellow", 2f);
}
void MakeYellow ()
{
CubeColor.color = Color.yellow;
Invoke("MakeRed", 2f);}
void MakeRed ()
{
CubeColor.color = Color.red;
Invoke("MakeGreen", 2f);
}
}