I am making a 2d platformer and when the player falls off the edge, i have to re run the game. what script do I write if I want it so when I hit "r" it restarts the scene. This is what I am using now: using System.Collections; using System.Collections.Generic; using UnityEngine; using SceneManagement; public class RestartGame : MonoBehaviour { void Update() { if(Input.GetKeyDown(KeyCode.R)) { SceneManager.LoadScene(0); } } }
You could put a collider below the level, with a script on it, that also calls SceneManager.LoadScene(0) in the OnCollision() function. I guess you should set IsTrigger on it.
Here is my restart script: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine; public class RestartScene : MonoBehaviour { void Update() { if (Input.GetKey("t")) { Restart(); } } void Restart(){ SceneManager.LoadScene(SceneManager.GetActiveScene().name); } }
You've asked a question and presented a solution of your own making which does just that. What is the issue?
In general a lack of information for any real reply to be given. In the event the previous replies have not solved your problem, I hope the following questions can string along more details about your situation. How did it not work? error logs if any will be helpful Did you attach the script to a gameobject in your scene? Is scene at index 0 of the build settings, the scene you are intending to reload? Is the scene you intend to reload in build settings at all? The basic nature of these questions is not meant to insult, just to eliminate or narrow possibilities. But again, if your problem has been solved, nevermind.
this guy actually has it right but remember if your scene is in a folder be sure to include the foldername aswel like this : (my scenes are in a folder named "Scenes") carefull the foldername is case sensitive Code (CSharp): public void RestartScene() { SceneManager.LoadScene("Scenes/"+SceneManager.GetActiveScene().name); }