So I’m trying to make a health script which works for the time being but when the health reaches zero I want the game to restart heres the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
public Text Health_bar;
private int Health_atm = 0;
public PlayerMovement movement;
// Start is called before the first frame update
void Start()
{
Health_atm = 100;
Health_bar.text = "Health:" + Health_atm;
}
public GameManager gameManager;
private void Update()
{
if(Health_atm == 0 || Health_atm > 0){
movement.enabled = false;
FindObjectOfType<GameManager>().Restart();
}
}
public void OnCollisionEnter(Collision collision) {
if(collision.gameObject.name == "Enemy"){
Health_atm = Health_atm - 50;
Health_bar = "Health:" + Health_atm.ToString();
}
}
}
and the GameManager code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public void Restart(){
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Photo:Unity Problem - Album on Imgur wont let me post image so heres the link.
any help is appreciated