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;
}
}
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.
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?
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 :)
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 ;)
@$$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?
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.
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.
@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.
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 ;)
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?
@$$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
Follow this Question
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