using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
Player player;
// Use this for initialization
void Start ()
{
player = FindObjectOfType();
}
// Update is called once per frame
void Update ()
{
if (player.isDead)
{
Invoke("RestartGame", 5);
}
}
public void RestartGame()
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
}
this code was supposed to restart the game 5 seconds later right after my character’s health hits zero ( I mean right after it’s hp reaches zero, sorry for my english by the way. )
but it’s not working and I’ve just started to using unity and c#, so I don’t reall know what’s wrong.
Can anyone help me?
@erdmkoc
I noticed that your player isnt actually set to find any type. Why dont you set it like this:
public class GameManager : MonoBehaviour { Player player;
// Use this for initialization
void Start () {
//This is the line changed
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update ()
{
if (player.isDead)
{
Invoke("RestartGame", 5);
}
}
public void RestartGame()
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
}
I’m assuming that the problem is that the scene is not added to the build settings, so it cannot reload it (you can only load scenes that are added to the build settings).
To solve this, in File menu click on Build Settings, and add your scene to the list of scenes (easiest way is to click “Add Open Scenes”).
Btw, the next time please try to describe what doesn’t work, describing what is the behaviour that you see in contrast to the expected behaviour. It helps a lot determining what is the issue.