Guide:Unity 3D scenes
Scripts for working with scenes, which includes changing levels, resetting, and quitting.
You can attach these to any game object in your project.
Examples
reset.js
import UnityEngine.SceneManagement; function Update() { if (Input.GetKeyDown(KeyCode.Backspace)) { SceneManager.LoadScene(1); } }
You can replace KeyCode.Backspace
with the name of whichever key you wish. You could also replace it with, for example "fire1"
or "jump"
, whose keys are determined at edit>project settings>input, and may also be easily reconfigured by players (if given the option). IE:
import UnityEngine.SceneManagement; function Update() { if (Input.GetButtonUp("Reset")) { SceneManager.LoadScene(1); } }
You can replace the 1
in SceneManager.LoadScene(1)
with the number of whichver level you want to load. You can find the number by going to the edit>build settings. Make sure to include all your scenes at the this point! You can also replace it with a variable you can edit in the inspector, so that you can easily select which level to reset to, and can reset to different levels depending on where the player is without having to duplicate your script. IE:
import UnityEngine.SceneManagement; var scene = int; function Update() { if (Input.GetButtonUp("Reset")) { SceneManager.LoadScene(scene); } }
Most recently I used something called an enumerator, so that I could select scenes via a drop-down menu in the inspector, and have all the scenes labeled with something understandable so I didn't have to remember numbers. IE:
import UnityEngine.SceneManagement; enum scene { Logo = 0, Title = 1, Intro = 2, Game = 3, Gameover= 4, Ending = 5 } var resetToScene : scene; function Update() { if (Input.GetButtonUp("Reset")) { SceneManager.LoadScene(resetToScene); } }
The 'enum' is laid out like that just for clarity's sake. You can assign whatever numbers you want to the entries (Logo, Title, etc.). In fact I could have left the numbers out, as they are assigned sequential numbers by default. So
enum scene {Logo,Title,Intro,Game,Gameover,Ending}
would work just the same.
quit.js
function Update() { if (Input.GetButtonUp("Quit")) { Application.Quit(); } }
Like reset.js just attach this to any object and it should work. This is already setup to allow the user to configure the quit key themselves.