Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Ray116GA · Apr 25, 2016 at 06:00 PM · c#levelreloaddeathrestart

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 (:/)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Apr 27, 2016 at 06:26 PM

@Ray116GA

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();
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ray116GA · Apr 28, 2016 at 08:28 AM 1
Share

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.

avatar image FWCorey Ray116GA · Apr 28, 2016 at 10:26 PM 0
Share

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

avatar image
0

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");
             }
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ray116GA · Apr 26, 2016 at 07:58 PM 0
Share

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.

avatar image Ray116GA · Apr 26, 2016 at 10:19 PM 0
Share

btw i already created a button that is a child of canvas.

avatar image
0

Answer by Muntinue · Apr 26, 2016 at 08:33 PM

@Ray116GA

I think you would use this instead.

 SceneManagement.GetActiveScene();

Hope it works.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ray116GA · Apr 27, 2016 at 05:28 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges