I’m trying to make a toggle button so that when the timer reaches 0 the toggled device will turn on.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CountdownTimerManager : MonoBehaviour {
public AudioClip Alarm;
float timer = 3600;
bool isFinishedTimer = true;
public UnityEngine.UI.Text displayText;
public UnityEngine.UI.Text timeText;
string minsDisplay;
string secsDisplay;
int mySeconds = 0;
int sysHour = System.DateTime.Now.Hour;
private float oldTimer;
//Random Clips
public AudioClip[] voices;
public AudioClip[] timeVoices; //Noon Time
public AudioClip[] timeVoices2; //Morning Time
public AudioClip[] timeVoices3; //Night Time
public static CountdownTimerManager countDownManager;
void Awake () {
if (countDownManager == null) {
DontDestroyOnLoad (gameObject);
countDownManager = this;
} else if (countDownManager != this) {
Destroy (gameObject);
}
//Time OF Day Notification
//Noon Time
if (sysHour == 12) {
GetComponent<AudioSource>().clip = timeVoices[Random.Range(0,timeVoices.Length)];
GetComponent<AudioSource>().Play();
Debug.Log ("Good Afternoon!");
}
//Morning Time
else if (sysHour == 8) {
GetComponent<AudioSource>().clip = timeVoices2[Random.Range(0,timeVoices2.Length)];
GetComponent<AudioSource>().Play();
Debug.Log ("Good Morning!");
}
//Night Time
else if (sysHour == 22) {
GetComponent<AudioSource>().clip = timeVoices3[Random.Range(0,timeVoices3.Length)];
GetComponent<AudioSource>().Play();
Debug.Log ("Good Night!");
}
else{
//Do something if desired
Debug.Log("Go to bed!");
}
}
void Start(){
oldTimer = timer;
}
void Update(){
if (!isFinishedTimer) {
timer -= Time.deltaTime;
}
CurrentTime();
}
void CurrentTime() {
System.DateTime dt = System.DateTime.Now;
int h = dt.Hour;
int m = dt.Minute;
int s = dt.Second;
timeText.text = h + ":" + m + ":" + s;
if(mySeconds != s)
{
mySeconds = s;
//Timing();
StartCoroutine(Timing());
}
}
IEnumerator Timing()
{
if (timer > 0) {
//var minsDisplay : String = parseInt( timer / 60 ).ToString();
minsDisplay = System.Convert.ToInt32( timer / 60 ).ToString();
//var secsDisplay : String = parseInt( timer ).ToString();
secsDisplay = System.Convert.ToInt32( timer ).ToString();
if ( (timer - ( System.Convert.ToInt32(minsDisplay) * 60)) > 10 ) {
secsDisplay = System.Convert.ToInt32( timer - ( System.Convert.ToInt32(minsDisplay) * 60) ).ToString();
}
else {
secsDisplay = "0" + System.Convert.ToInt32( timer - ( System.Convert.ToInt32(minsDisplay) * 60) ).ToString();
}
//displayText.text = minsDisplay + " : " + secsDisplay;
}
//Timer Reaches End We Can Do Something Here
else {
timer += oldTimer;
GetComponent<AudioSource>().PlayOneShot(Alarm);//Plays Alarm Sound
isFinishedTimer = true;//Sets Inspector Value to true or false based on what is set here
yield return new WaitForSeconds(5.8f);//Wait Time Setting
//Do Something if Desired
//if (GetComponent.<AudioSource>().isPlaying) return; // don't play a new sound while the last hasn't finished
GetComponent<AudioSource>().clip = voices[Random.Range(0,voices.Length)];
GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(0.8f);//Wait Time Setting
//Do Something if Desired
//Sending.sendBlue ();
//Sending.sendRed ();
Debug.Log ("Timer Ended");
}
displayText.text = minsDisplay + " : " + secsDisplay;
}
//Timer Stop Button
public void GoTimerStop()
{
isFinishedTimer = true;
}
//Timer Start Button
public void GoTimerStart()
{
isFinishedTimer = false;
}
//Timer Settings
public void GoTimerSetting60Sec()
{
timer = 60;
}
public void GoTimerSetting5Min()
{
timer = 300;
}
public void GoTimerSetting10Min()
{
timer = 600;
}
public void GoTimerSetting20Min()
{
timer = 1200;
}
public void GoTimerSetting30Min()
{
timer = 1800;
}
public void GoTimerSetting40Min()
{
timer = 2400;
}
public void GoTimerSetting50Min()
{
timer = 3000;
}
public void GoTimerSetting1Hr()
{
timer = 3600;
}
public void GoTimerSetting1Point5Hr()
{
timer = 5400;
}
public void GoTimerSetting2Hr()
{
timer = 7200;
}
public void GoTimerSetting2Point5Hr()
{
timer = 9000;
}
public void GoTimerSetting3Hr()
{
timer = 10800;
}
//Device One Toggle
public void ToogleDeviceOne() {
//Device Set to go off with timer Here
if (timer < 0) {
isFinishedTimer = true;
Sending.sendBlue();
}
else {
//Something
}
}
}