Restart current level after deathscreen
Hi im working on a little 2D-Platformer and made a deathscreen on which the Player gets when he collides with an enemy. The only Problem i have now is that the restart button wont reload the current Level. Pls help me im tried almosst everything now. here my script for enemies : using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
 5. public class CollisionDeath : MonoBehaviour {
  
    public GameObject DeathScreen;
    public GameObject thePlayer;
    public GameObject Spikes;
 10.   private bool deathScreen;
    public string LevelToLoad;
    // Use this for initialization
    void Start () {
      DeathScreen.gameObject.SetActive(false);
 15.     }
    void OnCollisionEnter2D(Collision2D other)
    {
      if (other.gameObject.name == "Mario")
      {
 20.       Debug.Log("wawawa");
        thePlayer = other.gameObject;
        other.gameObject.SetActive(false);
        DeathScreen.gameObject.SetActive(true);
        SceneManager.LoadScene(LevelToLoad); //Application.LoadLevel("DeathScreen");
 25.       Time.timeScale = 0f;
        thePlayer.gameObject.SetActive(true);
      }
    }
  }
and the script for the deathscreen:
 using UnityEngine;
  using System.Collections;
  using UnityEngine.SceneManagement;
  
 5. public class DeathScreen : MonoBehaviour
  {
    public string Restart;
    public void restartGame ()
    {
 10.     Debug.Log("restart1");
      Time.timeScale = 1f;
      Debug.Log("restart");
      int scene = SceneManager.GetActiveScene().buildIndex;
      SceneManager.LoadScene(scene, LoadSceneMode.Single);}}
I also set an onlick() on my button that goes to the function DeathScreen.restartGame (:/)
Answer by TBruce · Apr 27, 2016 at 06:26 PM
Here are functions for loading the current scene, the previous scene and the next scene in a game
 public void RestartScene()
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }
 
 public void LoadPreviousScene()
 {
     if (SceneManager.GetActiveScene().buildIndex > 0)
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
     }
     else RestartScene();
 }
 
 public void LoadNextScene()
 {
     if ((SceneManager.GetActiveScene().buildIndex + 1) < SceneManager.sceneCount)
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
     }
     else RestartScene();
 }
Holy f* s* snacks, it works!!!!!!!! arigatou gozaimasu $$anonymous$$avina-Senpai!!!!!!! I would rather create 10 fakes accounts to upvote this post with each, but i guess i would get banned XD
Edit for multiple levels/scenes:
Now i only have to create for every level/scene a own deathscreen, which is one above the level/Scene in buildIndex, so it can jump -1 in buildIndex.
Therefor i must declare in LoadNextScene() for buildIndex +2, so it skips the Deathscreen for each level/Scene.
You can simply create an Enum that contains your list of scenes and use
Scene$$anonymous$$anager.LoadScene($$anonymous$$yScenes.DeathScene);
To load your death scene. It also makes it easy on a level select scene since you can configure buttons with a dropdown menu to load a specific scene.
The only downside is you have to be vigilant in keeping the enum up to date when creating new scenes
Answer by Zoogyburger · Apr 25, 2016 at 06:12 PM
This script will create a button that will load level on click:
     void OnGUI()
     {
         const int buttonWidth = 120;
         const int buttonHeight = 60;
             
         if (
             GUI.Button(
             // Center in X, 1/3 of the height in Y
             new Rect(
             Screen.width / 2 - (buttonWidth / 2),
             (1 * Screen.height / 3) - (buttonHeight / 2),
             buttonWidth,
             buttonHeight
             ),
             "Retry!"
             )
             )
             {
             // Reload the level
             SceneManager.LoadScene("YOUR SCENE NAME");
             }
     }
 }
actually i want to reloadthe last scene i have used. I planing severale levels that all get to the deathscreen if the player dies. if i declare the scene name it will jump from any scene where i die, to the scenewhich i ut the name to it,if i am getting this right.
btw i already created a button that is a child of canvas.
Answer by Muntinue · Apr 26, 2016 at 08:33 PM
I already tried this, as you can see in my uploaded script. i think the Problem with this is that it gets the "activeScene" in this case the active deathscreen from where i try to get to the last played Level :p
But thanks for your effort mate!
Your answer
 
 
             Follow this Question
Related Questions
How can i restart a script on collision ? 1 Answer
death after collision,Tod nach Kollision 0 Answers
script help 1 Answer
How to add a restart to my 2D game? 0 Answers
Gun not reloading properly & time not deducting correctly when below 10 seconds 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                