C# - How to close the game after x seconds

[Solved]

I’m making a game, and I am an absolute beginner (I know no C# whatsoever). I want to know how I can make a script that after x seconds, the game closes itself. I don’t want any key inputs; I just want to stare at the scene, and then it closes the game automatically.

Look into Invoke or Coroutines.

this?

void Start()
    {
        StartCoroutine(DelayLittle());
    }

    IEnumerator DelayLittle()
    {       
        yield return new WaitForSeconds(5); //wait 5 secconds
        Application.Quit();
    }

Perfect! Thank you. It’s also coincidentally the exact amount of seconds I needed.

does it close after falling out of the map?

Please don’t necro-post. If you have a new question, make a new post. It’s FREE!!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • ONLY post the relevant code, and then refer to it in your discussion.

I think that’s our new generation “cut and paste a sentence fragment from elsewhere” kind of reply bot.

1 Like