- Home /
Reload scene when dead
What i want is that when life is zero, that the scene just restarts, but i can't find out how to do this
i now have something like this:
var playerLife = 100;
if (playerLife < 1){ //Reload scene }
Answer by Jessy · Feb 09, 2011 at 06:26 PM
http://unity3d.com/support/documentation/ScriptReference/Application.LoadLevel.html
http://unity3d.com/support/documentation/ScriptReference/Application-loadedLevel.html
if (playerLife < 1) Application.LoadLevel(Application.loadedLevel);
Thats not really working, but it doesn't show any errors so i can't see what's wrong..
Never $$anonymous$$d, solved it already, had to put a function update around it
Answer by ArtOfWarfare · Dec 09, 2015 at 04:20 AM
Application.LoadLevel()
is now obsolete, so you should use SceneManager.LoadScene()
instead. You can pass in an argument of SceneManager.GetActiveScene()
to just reload the current scene.
Note that unlike the Application
class, the SceneManager
class is not directly in UnityEngine
, so you'll need to add this line to your imports at the top of your source file: using UnityEngine.SceneManagement;
.
Answer by lolmaster2 · Jan 23, 2017 at 08:35 AM
In Unity 5.0 and later
// place this at the top of your file
using UnityEngine.SceneManagement;
// then call this to restart game
void hardRestartGame() {
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
Answer by RealEfrain12 · Nov 24, 2020 at 04:30 AM
Unity 2019.4 Edit: This is C# Edit 2: also have the script named "TriggerControl" otherwise it's gonna give you an error This is really late but I just want to post this, put this script in to the object that you want to reload the scene when the player and the object collide, make sure the player or the object have a rigidbody.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TriggerControl : MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Player")
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Answer by LeHombreDeZbragl · Feb 01, 2021 at 10:10 PM
I had this problem too. I wanted to destroy the player object and than restart the scene, but then i realized, that i was calling the code from object that was already destroyed. So just create empty object and attach the code to it. Hope this might help someone :)
Your answer
Follow this Question
Related Questions
GameManager and scene design issue. 1 Answer
Problem with Application.Loadedlevel 0 Answers
MissingReference When Reloading Scene 1 Answer
Application.LoadLevel & Time.timeScale glitch 1 Answer
Reset a scene 2 Answers