Search Unity

How to script Level Restarting if R is down?

Discussion in 'Scripting' started by bluedogtm, Jan 16, 2017.

  1. bluedogtm

    bluedogtm

    Joined:
    Jan 16, 2017
    Posts:
    12
    using UnityEngine;

    public class RestartGame : MonoBehaviour

    void (input.GetKeyDown(varKeyInput)
    {
    Application.LoadLevel(?);
    }

    //I know its wrong
     
  2. bluedogtm

    bluedogtm

    Joined:
    Jan 16, 2017
    Posts:
    12
    *If I press key "R" game restart*
     
  3. Jam0kid

    Jam0kid

    Joined:
    Nov 6, 2010
    Posts:
    19
    There are two ways of doing it; asynchronously and synchronously. The latter will hang the application, but it's way fewer steps to get working. So I'll put that one first and then talk about an async call later.

    First, if you haven't already, add your scene to the list of scenes by going File:Build Settings and dragging your scene into the box list.

    Second, in your script, write something like the following.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using SceneManagement;
    4.  
    5. public class RestartGame : MonoBehaviour
    6. {
    7.     void Update()
    8.     {
    9.         if(Input.GetKeyDown(KeyCode.R))
    10.         {
    11.  
    12.             //Application.LoadLevel is depreciated; use the scene manager function
    13.             //that you get from 'using SceneManager'
    14.             //You can see there are a few overloads for this function; you can pass a string (name) or number for the scene.
    15.             SceneManager.LoadScene(0);
    16.         }
    17.     }
    18. }
    That would be the way that (if you have a massive scene to move to) will hang your application for some time. Most people will be okay with waiting a little bit, as long as they know they haven't crashed, so the other alternative would be preferable: LoadSceneAsync

    Code (CSharp):
    1. bool m_transitioning = false;
    2.  
    3.     void Update ()
    4.     {
    5.         if(Input.GetKeyDown(KeyCode.R) && !m_transitioning)
    6.         {
    7.             StartCoroutine(ILoadLevel());
    8.         }  
    9.     }
    10.  
    11.     //Google Unity Coroutines if you are unsure about this, mate
    12.     IEnumerator ILoadLevel()
    13.     {
    14.         //Don't forget to lock the coroutine from multiple calls.
    15.         m_transitioning = true;
    16.  
    17.         //Calling this gets you the progress, completion and some stuff like that to tinker with while loading.
    18.         AsyncOperation operation = SceneManager.LoadSceneAsync(0);
    19.  
    20.         //From here you can say something like -   while(!operation.isDone) progressText = operation.progress;
    21.      
    22.         //Finally, change scenes once we're done loading.
    23.         yield return operation;
    24.     }
    If you have any other queries, don't hesitate to reply.

    -edit-
    SceneManagement, not SceneManager.
     
    bluedogtm likes this.
  4. bluedogtm

    bluedogtm

    Joined:
    Jan 16, 2017
    Posts:
    12
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    To add to @Jam0kid 's post, you can also use SceneManager.GetActiveScene() to get the current scene and then pass it's name to the SceneManager.LoadScene(Async()) function. That way you don't need to use magic numbers to reload the scene.
     
    bluedogtm likes this.