Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Dec 01, 2015 at 08:27 PM · c#uitimer-scripttoggle buttontimers

Toggle Button For Timed Device When Timer Runs Out

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
 
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KnightRiderGuy · Dec 02, 2015 at 02:37 PM

I ended up solving the issue here: Solution Here

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Count Down Timer C# Conversion Script Not Working Help 3 Answers

Latching Toggle Button Help 2 Answers

Remember Toggle Button State When Scene Return 0 Answers

Toggle UI Text With UI Toggle Button 1 Answer

Start and Stop Timer Help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges