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!
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.