- Home /
Restart button for multiple scenes?
I am new to programming, and I wanted to create a multiple level or scenes for my game. First I made a scene for a game over screen and a button for restarting. My problem is that I don't know the code or a way for this restart button to restart the game without going back to other scenes. Here's an example: I died on level 1, game over screen shows up, I then presses the restart button, it goes to level 1 instead.
If there is a way for it to go back please do tell. Thank you!
Answer by In0sc0p3dJFK · May 16, 2021 at 11:30 AM
Just reload the scene.
Firstly, you're going to have to add a new directive. If you're not familiar with what that is, it's basically just the little lines of code at the veryyy top of your script such as:
using UnityEngine;
using System.Collections;
//code
You're going to want to add the Unity.SceneManagement directive. so you can just add it under the other directives like this:
using UnityEngine;
using System.Collections;
using Unity.SceneManagement;
// code
If you were to give your code, this would be alot easier. But in whatever void or anything that triggers the Game Over scene, you have to just call the same scene again.
using UnityEngine;
Using System.Collections;
Using Unity.SceneManagement;
void GameOver()
{
SceneManager.LoadScene(SceneManager.GetActiveScene());
}
I'm just assuming you have a void for the GameOver. Also, i'm assuming you want the game over screen on the screen for a couple of seconds before the scene restarts, so for that, you're going to want to use a Coroutine. But I wont get into that unless you need me to. If you have something like a scrolling background, the background might not reset with the scene so for that you might want to use Time.time and Time.deltaTime. But again, I won't get into that unless you really need it. I'm just assuming alot of stuff here. You might want to give us a some code as an example.
Hello, thank you for answering, but the GetActiveScene part won't go back to the first level because the active scene is the game over screen, and I'm sorry that I didn't revise my question but the one I'm asking is how to restart the scene based on what level the player is currently in.
using UnityEngine;
using UnityEngine.SceneManagement;
public class RestartButton : MonoBehaviour
{
public int Index;
public void ResetScene()
{
SceneManager.LoadScene(Index);
}
}
This is the script that I'm using.
oh ok. So you have to actually set the scene index outside of the script in unity. At the top left of your Unity project, there should be a a button called "Files". This website, for some reason, doesn't let me attach files so work with me here. It should drop down to reveal a bunch of options. One of the last options, there should be one called "Build Settings". If you open that up, there should be another window that pops up. It says "Scenes in Build". Drag all of your scenes from your project tab to that space there, in order. And if you already have a Build Index set up, it should start working if you use that same line of code you just showed. THIS WILL ONLY WORK if you are only using the game scene, and the game over scene. Any other scene put into this build will not work. And you're going to to have to load the first scene again.
Also, I noticed that you are using a button to restart? You need to add a void named something like "OnClickResetScene". But I wont get into that if you don't need me to.
I actually have done this, and it works perfectly, if I only have a single level. But my question is, is there a script or code for restarting the game based on what level the player is in. Is the script I'm using not applicable for having multiple levels/scenes on a single game?
Your answer
![](https://koobas.hobune.stream/wayback/20220613033802im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Simple Restart not working 0 Answers
Creating a Restart Button 3 Answers
scene won't reset after restart 1 Answer
My game keeps restarting at random 1 Answer
Full scene restart 1 Answer