Lose Screen won't show up when hp=0.
Hello, I am trying to call lose screen when hp is 0 it does not work. can you please give me some advice?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoseScreen : MonoBehaviour
{
public static bool GameLost = false;
public Health myhealth;
public GameObject loseScreenUI;
void Start()
{
GameLost = false;
myhealth = GameObject.Find("Player").GetComponent<Health>();
}
void Update()
{
if (myhealth.health <= 0)
LoseGame();
}
void LoseGame()
{
loseScreenUI.SetActive(true);
Time.timeScale = 0f;
GameLost = true;
}
public void RestartGame()
{
Time.timeScale = 1f;
GameLost = false;
SceneManager.LoadScene("Game");
}
}
Comment
Answer by lvskiprof · Aug 18, 2020 at 06:53 PM
In your code here loseScreen is never set, but you have declared it as a public value, so maybe it is set elsewhere in another script?
You should not be having public values set externally to a script. It is better to have a local method that sets it, either by calling it with a reference to the object instance, or you do a search for it in your Start() method.
I would also be concerned that you are setting a flag in the wrong instance, if more than one get created.