Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

game loads but quit in 1 second after adding a timer count down script

Discussion in 'Scripting' started by Ruckrova, Dec 18, 2014.

  1. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    I would like the game to quit or restart after the timer counts down to 0

    here is the code I used .

    the countdown works in the play mode but after building a game it quits in the first second while there is 60 second on the counter . any ideas ?

    thanks in advance

    Ruck

    #pragma strict

    var timer : float = 60.0;

    function Update()
    {
    timer -= Time.deltaTime;

    if(timer <= 0)
    {
    timer = 0;
    Application.Quit();
    }
    }

    function OnGUI()
    {
    GUI.Box(new Rect(10, 10, 50, 20), "" + timer.ToString("0"));
    }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    timer is public
    Have you changed the value in the inspector (it will override the = 60f )
     
  3. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    yes I changed it to a few different numbers to test but they all quit after the game loads , it something to do with this part of the script , its is not wait until 0 to do the next part of the script .

    the script runs but does the quit straight away , i tested it with a print statement and it prints log and it put it up straight away .

    if(timer <= 0)
    {
    timer = 0;
    Application.Quit();
    }

    PS it a java script not C+


    I will try we loadlevel to try to get that to work
     
    Last edited: Dec 19, 2014