Search Unity

How to detect if the game was killed

Discussion in 'Scripting' started by Dizamok, Oct 31, 2014.

  1. Dizamok

    Dizamok

    Joined:
    Jul 10, 2013
    Posts:
    4
    Hi,
    Is there an event that is sent to the game when its process is being killed (via Window's TaskManager for example) ?

    Sadly, OnApplicationQuit() isn't called in that case.
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Probably not. OnApplicationQuit() should only work when Application.Quit is being call.
    I really doubt there is a way to have control over when the user forces quit the application.

    Because essentially that's what it is: stopping a process through the Window's TaskManager is force killing an application not matter what.
     
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    You could tell upon starting the game up again, but not during the actual kill process. Killing it in that manner is not clean and won't exit smoothly, it just dies.
     
    eelstork likes this.
  4. Karma1989

    Karma1989

    Joined:
    Oct 16, 2012
    Posts:
    18
    better to build a file in c# that is called and run when the game starts and still runs after its closed to detect if its running or not?
     
    eelstork likes this.
  5. Karma1989

    Karma1989

    Joined:
    Oct 16, 2012
    Posts:
    18
    example make a custom launcher in c# that runs that kinda code and keeps running during game runtime if the game is killed the launcher will detect it and do something.
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Do this on game startup

    Code (CSharp):
    1.  
    2. if(PlayerPrefs.HasKey("GameKilled")){
    3. int killedVal = PlayerPrefs.GetInt("GameKilled");
    4. if(killedVal == 1){
    5. //The user killed the game
    6. }
    7. }
    8. PlayerPrefs.SetInt("GameKilled",1);

    and On Quit

    Code (csharp):
    1. PlayerPrefs.SetInt("GameKilled",0);
    Then you can detect if the user has killed the game on the last run