Search Unity

Restart Scene Key

Discussion in 'Scripting' started by AthenasAvenger, Jan 20, 2020.

  1. AthenasAvenger

    AthenasAvenger

    Joined:
    Jan 19, 2020
    Posts:
    3
    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);
    }
    }
    }
     
  2. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    224
    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.
     
  3. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Here is my restart script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine;
    5.  
    6. public class RestartScene : MonoBehaviour
    7. {
    8.     void Update()
    9.     {
    10.         if (Input.GetKey("t")) {
    11.             Restart();
    12.         }
    13.     }
    14.  
    15.     void Restart(){
    16.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    17.     }
    18. }
    19.  
     
    martintas7 likes this.
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    You've asked a question and presented a solution of your own making which does just that. What is the issue?
     
  5. AthenasAvenger

    AthenasAvenger

    Joined:
    Jan 19, 2020
    Posts:
    3
    Thank you so much! this was very helpful.
     
  6. AthenasAvenger

    AthenasAvenger

    Joined:
    Jan 19, 2020
    Posts:
    3
    My script didn't work
     
  7. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    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.
     
  8. GreatSilaS

    GreatSilaS

    Joined:
    Feb 18, 2021
    Posts:
    1
    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):
    1.     public void RestartScene()
    2.     {
    3.         SceneManager.LoadScene("Scenes/"+SceneManager.GetActiveScene().name);      
    4.     }
     
    FatPengu likes this.
  9. FatPengu

    FatPengu

    Joined:
    Sep 19, 2022
    Posts:
    1
    Thanks for the reminder!
    I was stuck on this script for like 30 min :D