Skip to content

Instantly share code, notes, and snippets.

@samsheffield
Created October 9, 2020 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsheffield/246944588aa0d328ffd34bb844475970 to your computer and use it in GitHub Desktop.
Save samsheffield/246944588aa0d328ffd34bb844475970 to your computer and use it in GitHub Desktop.
Basic example of quit and reset functionality in Unity 3D
// Simple quit and escape functionality
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// First, add this...
using UnityEngine.SceneManagement;
public class QuitAndReset : MonoBehaviour
{
// Set your desired restart scene in the Inspector. This Scene must be added to the Build Settings
public string restartToScene;
// Update is called once per frame
void Update()
{
// Application.Quit(): https://docs.unity3d.com/ScriptReference/Application.Quit.html
// Make sure to set your Input Manager to include a Quit element. Hint: You can change Cancel to Quit in the Input Manager.
// Otherwise, this will throw an error. You can also delete || Input... if you intend to only use the keyboard
if (Input.GetKey("escape") || Input.GetButtonUp("Quit"))
{
Debug.Log("Quit");
// This won't work in the editor :)
Application.Quit();
}
// SceneManager.LoadScene(): https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
// Make sure to set your Input Manager to include a Restart element.
// Otherwise, this will throw an error. You can also delete || Input... if you intend to only use the keyboard
if(Input.GetKey(KeyCode.R) || Input.GetButtonUp("Restart"))
{
// Restart to a named scene. Be sure that it already exists and that you've spelled it correctly
SceneManager.LoadScene(restartToScene);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment