Search Unity

Script doesn't stop

Discussion in 'Scripting' started by padomu, May 9, 2014.

Thread Status:
Not open for further replies.
  1. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    Hi,

    If I press play, I output a Text "Timer reseted" each 2 seconds into the console. If I press paly again, to stop the game in Unity, the output remains. Why?

    Seems I kind of start a background process or something.

    Code (csharp):
    1. using UnityEngine;
    2. using Debug = UnityEngine.Debug;
    3. using System.Collections;
    4. using System.Timers;
    5. using System.Diagnostics;
    6.  
    7.  
    8. public class Countdown : MonoBehaviour {
    9.     private GameMaster gm;
    10.     private GUIText countdownLabel;
    11.     private Timer timer;
    12.     private Stopwatch stopwatch = new Stopwatch();
    13.  
    14.     void Start ()
    15.     {
    16.         gm = GameObject.Find("GameMaster").GetComponent<GameMaster>();
    17.  
    18.         countdownLabel = GameObject.Find("Countdown").GetComponent<GUIText>();
    19.  
    20.         timer = new Timer(2000);
    21.         timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    22.         timer.Enabled = true;
    23.  
    24.         stopwatch.Start();
    25.         }
    26.  
    27.     void Update()
    28.     {
    29.         countdownLabel.text = string.Format("{0}.{1}", stopwatch.Elapsed.Seconds, stopwatch.Elapsed.Milliseconds / 100);
    30.     }
    31.  
    32.     private void OnTimedEvent(object source, ElapsedEventArgs e)
    33.     {
    34.         Debug.Log("Timer restarted");
    35.         RestartCountdown();
    36.     }
    37.  
    38.     private void RestartCountdown()
    39.     {
    40.         gm.SwitchPlayers();
    41.         timer.Stop();
    42.         timer.Start();
    43.         stopwatch.Reset();
    44.         stopwatch.Start();
    45.     }
    46. }
     
    Last edited: May 9, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    ok

    adding

    Code (csharp):
    1.     private void OnApplicationPause() {
    2.         stopwatch.Stop();
    3.     }
    didn't help.
     
    Last edited: May 9, 2014
  4. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    Anyone has an Idea?

    Edit: OnDisable() worked but I don't see why.

    Why is it stil lrunning? Why do I have to disable the timer? Why doesn't Unity clean it up when closing?
     
    Last edited: May 9, 2014
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The Timer class you're using is not a part of Unity and therefore will be completely unaware that you've paused the game. That's why it keeps going regardless of whether you're paused or not.

    I'm not familiar with that particular class. It seems to me, though, that you were almost on the right track with your fix. However, it was Timer (not Stopwatch) that is calling the function, so Timer is the one you should pause on application pause.
     
    arkano22 likes this.
  6. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    StarManta

    Of course, stopwatch was the wrong one.Tiemr works in a multithreaded env. so I've to close its external thread. Isn't there a functionality that a multithreaded class needs kind of register itself on the env, in this case Unity?

    It makes sense, that Unity doesn't know of this process if it was never told. Kind of assumed, that there is a internal communication.

    Anyway, thanks. New to multithreading. ;)

    Solved it with

    Code (csharp):
    1.     void OnDisable()
    2.     {
    3.         timer.Close();
    4.  
    5.     }
     
  7. TheJam3s

    TheJam3s

    Joined:
    Aug 25, 2021
    Posts:
    3
    For Me
    Timer.EndInit(); Worked. In whatever Function.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,496
Thread Status:
Not open for further replies.