Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Wait then Close

Discussion in 'Scripting' started by thechubbymonkey, Jan 7, 2015.

  1. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    I need help making a script where after 20 seconds have passed the application closes. Nothing else. Anyone know how to do that. Please give me the script. It shouldn't be too hard I think.
     
    Last edited: Jan 7, 2015
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Code (csharp):
    1. void Start(){
    2.     StartCoroutine(CloseAfterTime(20));
    3. }
    4.  
    5. public IEnumerator CloseAfterTime(float t){
    6.     yield return new WaitForSeconds(t);
    7.     Application.Quit();
    8. }
    Unless you are looking for a timeout feature? Where actions reset that 20 second timer, and you only want to close after 20 seconds of inaction?

    PS code typed right into the browser - check for any possible compilation issues.
     
  3. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    No, I just need it to wait 20 seconds, so this should work I think,
     
  4. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    For some reason your script is not working. Are you sure you did it right? Am I doing anything wrong. I just need the game to wait 20 seconds and then it will close. Also the console say I doesn't know what IEnumerator is?
     
  5. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    Does anyone else know how to do this? Anyone???
     
  6. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    You need to throw the code in a monobehavior script that is attached to some gameobject in the scene.

    using System.Collections

    Should be at the top, you need it to use IEnumerator, though it comes standard on a new C# script in unity.
     
  7. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    Sorry I am I bit confused. So this is a C# script.
     
  8. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    System.Collections

    void Start(){
    StartCoroutine(CloseAfterTime(20));
    }

    public IEnumerator CloseAfterTime(float t){
    yield return new WaitForSeconds(t);
    Application.Quit();
    }

    This is my C# script that I put in one of my objects in the game. It won't work.
     
  9. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    You should be creating a new C# script, which gives you a template monobehavior class named the same name as your script name. Start and Update should already be stubbed functions inside. Simply remove the Start and Update methods, put the code there inside that class, and it should compile. Then add that script as a component to an empty gameobject in your scene.

    If you aren't following what I am saying, http://unity3d.com/learn might be a useful link to check out. It sounds like you have very little experience with scripting in Unity, that would be a good place to get started. Even the very first scripting tutorial might be enough to teach you what you need to know for this.
     
  10. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63
    Yeah, I am a bit lost, but I will go check out the learn page. Thanks so much for giving me a few tips on how to do this. :D
     
  11. thechubbymonkey

    thechubbymonkey

    Joined:
    Jan 4, 2015
    Posts:
    63

    Finally got it, this is all it was,


    yield WaitForSeconds (25);

    Application.Quit();

    Wow, I lost a lot of time trying to get that!!! LOL!
    Thanks for the help!
     
  12. jsneuro

    jsneuro

    Joined:
    Oct 8, 2019
    Posts:
    1
    Another noob here. Tried all the solutions suggested here. None give errors and allow me to run play mode, but also none lead to the quiting of the game after x seconds. Is Application.Quit() supposed to work in the Unity Game mode?
    Or only when running a built game?
    And can this code simply be attached to any Game Object? I am now attaching it to my FirstPersonCharacter. Any help very much appreciated!
     
  13. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Application.Quit will only work in a standalone build. It will be ignored in the Unity editor.
    See: https://docs.unity3d.com/ScriptReference/Application.Quit.html

    Yes.