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 · Nov 29, 2015 at 05:26 PM · c#uicountdowntimer-scripttimers

Count Down Timer C# Conversion Script Not Working Help

OK I had this converted from a Java script I had which worked fine, My only problem with the Java script was that I could not find a way after hours of searching of getting my java script to send a message to another script that was written in C#. After much pain I decided to ask if it could be converted into C# I don't get any errors but the code does not seem to work. Can someone with way more code experience than I take a look and see if you see anything out of the ordinary as to why it would not work.

I'm using UI text and NOT on GUI text if that information helps any?

 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;
     
     private float oldTimer;
     
     //Random Clips
     public AudioClip[] voices;
     
     
     /*//Begin New
     function Awake ()
     {
         // Make LevelData a singleton, so we keep ourself alive and kill any clones
         if ( FindObjectsOfType(CountdownTimerManager).Length != 1 )     // If one of me already exists I must be a clone, so destroy me (don't feel bad about it)
             Destroy (gameObject);
         else        // I'm the original, so keep me around
             DontDestroyOnLoad (gameObject);
     }
     //End New
     
     
     /*function Awake () {
                DontDestroyOnLoad (transform.gameObject);
            }*/
     
     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();
         }
         
     }
     
     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();
             
             
             
             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;
     }
 }
 
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

3 Replies

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

Answer by Mrslayer01 · Nov 29, 2015 at 10:02 PM

ok so i got it to work. Basically you cannot just call an enumerator you have to do it like this:

         if (mySeconds != s)
         {
             mySeconds = s;
             StartCoroutine(Timing());
         } 

and it seems to work but the timer seconds is messed up on mine so let me know.

Comment
Add comment · Show 4 · 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
avatar image KnightRiderGuy · Nov 29, 2015 at 10:33 PM 0
Share

Thanks @$$anonymous$$rslayer01 it works but yes like you said the way it displays the count down seconds is weird, is there a way to fix that?

avatar image Mrslayer01 · Nov 29, 2015 at 10:59 PM 0
Share

Well i noticed it only happens when it displays $$anonymous$$uets. When it just displays seconds it counts down from 60 like normal so it something to do with this part:

  //var $$anonymous$$sDisplay : String = parseInt( timer / 60 ).ToString();
              $$anonymous$$sDisplay = System.Convert.ToInt32( timer / 60 ).ToString();
              
              //var secsDisplay : String = parseInt( timer ).ToString();
              secsDisplay = System.Convert.ToInt32( timer ).ToString();
              
              if ( (timer - ( System.Convert.ToInt32($$anonymous$$sDisplay) * 60)) > 10 ) {
                  secsDisplay = System.Convert.ToInt32( timer - ( System.Convert.ToInt32($$anonymous$$sDisplay) * 60) ).ToString();

I'll keep messing with it tho :)

avatar image KnightRiderGuy Mrslayer01 · Nov 29, 2015 at 11:04 PM 0
Share

Awesome @$$anonymous$$rslayer01 I REALLY apreciate this, code is not my strong point but I have no choice not knowing any reliable coders ;) I tossed a few points your way ;) You any good with Voice recognition stuff... kinda off topic I know but that is another bee in my bonnet so to speak... $$anonymous$$.I.T.T. needs his voice recognition and I'd give my left nut for a reliable coder that could help ;)

avatar image KnightRiderGuy Mrslayer01 · Nov 30, 2015 at 02:47 AM 0
Share

@$$anonymous$$rslayer01 I'm trying to figure out where it gets that:

 ToInt32

Value from? I would thing that should be an even number or something?

avatar image
0

Answer by Mrslayer01 · Nov 29, 2015 at 09:24 PM

I put the code into my scene and assigned a ui text to the text gameobject and it's displaying my computers time just fine. Sorry i'm just not understanding what the error is.

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
avatar image
0

Answer by KnightRiderGuy · Nov 29, 2015 at 09:34 PM

@Mrslayer01 The UI text shows up fine but when I try to use one of my buttons to assign a count down time nothing happens. Yes the "System Time" is working just fine, it the timer that is not working, the java version worked but I need this in C# in order to be able to talk to the other C# script that talks to the Arduino.

Comment
Add comment · Show 4 · 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
avatar image Mrslayer01 · Nov 29, 2015 at 09:43 PM 1
Share

@zentaiguy Well this part of the code:

  //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 GoTimerSetting5$$anonymous$$in()
     {
         timer = 300;
     }
 
     public void GoTimerSetting10$$anonymous$$in()
     {
         timer = 600;
     }
 
     public void GoTimerSetting20$$anonymous$$in()
     {
         timer = 1200;
     }
 
     public void GoTimerSetting30$$anonymous$$in()
     {
         timer = 1800;
     }
 
     public void GoTimerSetting40$$anonymous$$in()
     {
         timer = 2400;
     }
 
     public void GoTimerSetting50$$anonymous$$in()
     {
         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;
     }

does set the float "timer" to what it says but it seems like it's missing some code cause i assume it's supposed to change the timertext to a countdown and count down from whatever timer is set to. With this code tho nothing changes when you change the timer but let me keep looking at it and see if i can set something up.

avatar image KnightRiderGuy Mrslayer01 · Nov 29, 2015 at 09:50 PM 0
Share

Yes that right @$$anonymous$$rslayer01 there is something definite got snafued during the conversion because those buttons do not change the time, the start and stop buttons do not appear to do anything either altho its hard to tell with the buttons time setting not working.

Thanks for helping I REALLY appreciate this I have been at this one for like forever it feels like ;)

avatar image Mrslayer01 · Nov 29, 2015 at 09:55 PM 0
Share

Alright so after messing around with it i found that even tho this:

             if (mySeconds != s)
             {
                 //Debug.Log("working");
                 mySeconds = s;
                 Ti$$anonymous$$g();
             }

is working just fine but the problem comes in when it tries to call Ti$$anonymous$$g. I'm not exactly sure why this is not working but that seems to be the issue. Also you said it worked just fine when it was in java?

avatar image KnightRiderGuy Mrslayer01 · Nov 29, 2015 at 11:50 PM 0
Share

@$$anonymous$$rslayer01 yup, the java version of the script the timer worked normal. Now with your fixes it works but does count down a little funny with the (-) in front of the number ins$$anonymous$$d of the fractions of seconds.

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

42 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

Related Questions

Start and Stop Timer Help 1 Answer

Toggle Button For Timed Device When Timer Runs Out 1 Answer

How to get a countdown timer with values entered by the player 0 Answers

(C#) Execute script when button is held for 'x' seconds 2 Answers

How can I make a continue button with the following scripts 0 Answers


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