- Home /
Question by
Swishy96 · Apr 09, 2021 at 01:47 PM ·
level load
Im trying to make it so when the game ends it puts you back to the level you were on I currently have two levels and its not working. Heres my code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
bool gameHasEnded = false;
bool level2 = false;
public float restartDelay = 1f;
public float winDelay = 8f;
public GameObject completeLevelUI;
public void EndGame()
{
if(gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("Oops");
Invoke("Restart",restartDelay);
}
}
void Restart()
{
if(level2 == true)
{
SceneManager.LoadScene("Scene2");
}
if (level2 == false)
{
SceneManager.LoadScene("Scene1");
}
}
void Level2()
{
SceneManager.LoadScene("Scene2", LoadSceneMode.Single);
SceneManager.SetActiveScene(SceneManager.GetSceneByName("Scene2"));
SceneManager.UnloadSceneAsync("Scene1");
}
public void WinGame()
{
completeLevelUI.SetActive(true);
Debug.Log("WonGame");
Invoke("Level2", winDelay);
level2 = true;
}
}
Comment
When you say "it's not working" what exactly IS happening? Where is EndGame()
being called from?
Answer by Razputin · Apr 09, 2021 at 02:13 PM
If you're just loading the level you're already on you can just do
public void ResetScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
Then just call ResetScene when the game ends. You can add the async crap into there too.
Your answer
![](https://koobas.hobune.stream/wayback/20220613032039im_/https://answers.unity.com/themes/thub/images/avi.jpg)