System.Daignostic. Stopwatch Not working on Builds but Works in Editor 2017.2.0p2
Im under the gun with a project but i really would like a proper timer solution. Im unsure how to validate where the issue is occuring.`enter code here.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
//using System.Diagnostic;
public class Timer : GenericSingletonClass<Timer>
{
public float Time = 120;
[SerializeField]
public float elapsedTime;
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
public static Action timerFinished;
public bool isRunning;
private void OnEnable()
{
}
public void StartTimer()
{
watch.Start();
}
public void StopTimer()
{
watch.Stop();
watch.Reset();
}
public void ResetTimer()
{
watch.Reset();
StartTimer();
}
private void Update()
{
if(Input.touchCount > 0)
{
ResetTimer();
}
if(watch.IsRunning)
{
if(watch.Elapsed.Seconds >= Time)
{
StopTimer();
if(timerFinished != null)
{
timerFinished();
}
}
}
elapsedTime = watch.Elapsed.Seconds;
}
}
the reason i want to use this timer in this way is because i need to send events out on completion but it seems to never start in build. there aren't any error logs for it either.
other wise my boss wants to implement it this way`using System.Collections; using System.Collections.Generic; using UnityEngine; //using System.Diagnostics; using System;
public class Timer : GenericSingletonClass { public float IdleTime = 120; [SerializeField] public float elapsedTime; private bool stopped = false; //Stopwatch s = new Stopwatch(); //public static Action timerFinished;
public void StartTimer()
{
stopped = false;
//s = new Stopwatch();
//s.Start();
}
public void StopTimer()
{
//s.Stop();
//s = null;
stopped = true;
elapsedTime = 0f;
//UnityEngine.Debug.Log ("stopping timer--------------------------------------");
//s.Reset();
}
public void PauseTimer() {
stopped = true;
}
public void ResetTimer()
{
stopped = false;
elapsedTime = 0f;
//s = new Stopwatch();
//s.Reset();
//StartTimer();
}
public void ResetOnTouch() {
if (!stopped) {
ResetTimer ();
}
}
private void Update()
{
if(Input.touchCount > 0 || Input.GetMouseButtonDown(0))
{
ResetOnTouch();
}
if (!stopped) {
elapsedTime += UnityEngine.Time.deltaTime;
if (elapsedTime >= IdleTime) {
GlobalReset.Instance.ResetApp();
}
}
/*
if(s != null && s.IsRunning)
{
if(s.Elapsed.Seconds >= Time)
{
if (timerFinished != null) {
//timerFinished ();
GlobalReset.Instance.ResetApp();
}
StopTimer();
}
}
if(s != null)
elapsedTime = s.Elapsed.Seconds;
*/
}
}
which ads more functions to do the same work and while i can put my event for timer finished back into this i still want to find out whats wrong with Stopwatch.