- Home /
How to restart the game
I am looking for a way that I can restart the game with pressing the letter R without closing out of the application in standalone. I only have 1 scene and I have NO Experience. Please help me:D
Answer by doublemax · Oct 09, 2016 at 07:52 AM
using UnityEngine.SceneManagement;
SceneManager.LoadScene( SceneManager.GetActiveScene().name );
Answer by NikitaDemidov · Dec 31, 2018 at 03:00 PM
using UnityEngine;
public class Restart : MonoBehaviour {
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
Application.LoadLevel(0);
}
}
}
Try this!
I believe this should be the proper code in restarting your game as Application.LoadLevel(0);
gets the first scene of your game while the SceneManager.GetActiveScene().name
could probably gets the third level of your game which does not totally restart your game but just restarting that level. However the Code is obsolete and rather use SceneManager.LoadScene(0);
Answer by LiamDietrich · Oct 09, 2016 at 03:24 PM
@doublemax where do you put this line on code?
Save this as "Restart.cs" and add it to any component that's always active, e.g. the main Camera or a GameManager object if you have something like that.
using UnityEngine;
using UnityEngine.SceneManagement;
public class Restart : MonoBehaviour
{
void Update ()
{
if( Input.GetKeyDown(KeyCode.R) )
{
SceneManager.LoadScene( SceneManager.GetActiveScene().name );
}
}
}
Answer by cmdbarret · Dec 31, 2018 at 03:39 AM
@doublemax can I ask why this changes the games lighting?
I haven't used Unity for 2 years, but Googling found this: https://answers.unity.com/questions/1264278/unity-5-directional-light-problem-please-help.html
Your answer
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.