• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity
×
 

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel
    • Ask a question
    • Spaces
    • Home /
    avatar image
    1
    Question by LiamDietrich · Oct 09, 2016 at 07:48 AM · restart game

    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

    Add comment · Show 1

    4 Replies

    · Add your reply
    • Sort: 
    avatar image
    2

    Answer by doublemax · Oct 09, 2016 at 07:52 AM

    1. using UnityEngine.SceneManagement;

    1. SceneManager.LoadScene( SceneManager.GetActiveScene().name );
    Add comment · Share
    avatar image
    0

    Answer by NikitaDemidov · Dec 31, 2018 at 03:00 PM

    using UnityEngine;

    public class Restart : MonoBehaviour {

    1. void Start()
    2. {
    3. }
    4. void Update()
    5. {
    6. if (Input.GetKeyDown(KeyCode.R))
    7. {
    8. Application.LoadLevel(0);
    9. }
    10. }
    11. }

    Try this!

    Add comment · Hide 1 · Share
    avatar image vfxjex · Oct 28 at 12:33 PM 0
    Share

    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);

    avatar image
    0

    Answer by LiamDietrich · Oct 09, 2016 at 03:24 PM

    @doublemax where do you put this line on code?

    Add comment · Hide 1 · Share
    avatar image doublemax · Oct 09, 2016 at 03:29 PM 1
    Share

    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 );
         }
       }
     }
    
    avatar image
    0

    Answer by cmdbarret · Dec 31, 2018 at 03:39 AM

    @doublemax can I ask why this changes the games lighting?

    Add comment · Hide 1 · Share
    avatar image doublemax · Dec 31, 2018 at 01:31 PM 0
    Share

    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

    Hint: You can notify a user about this post by typing @username

    Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

    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.



    Follow this Question

    Answers Answers and Comments

    7 People are following this question.

    avatar image avatar image avatar image avatar image avatar image avatar image avatar image

    Related Questions

    ??Whats wrong with this script??? HELP 2 Answers

    Darker Level when Player Respawn 1 Answer

    Is there a way to return all your viaraibles and script to the way it was when the game started 0 Answers

    Reset Transform.position 1 Answer

    Buttons dissapear after restart, mulitple errors 0 Answers