SceneManager.LoadScene(GetActiveScene.name) isn't working and making 2 scenes

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

So… if the health is 0 or greater than 0, you want to restart? Is that really what you meant?

The image is not helpful. Please describe what is happening.

1 Like

The first if statement is incorrect. It should check for if(Health_atm <= 0)

1 Like

in the heiarchy the scenes are duplicated and say not loaded and thanks for pointing the health thing out ill fix it

I’d guess that the scene is continually restarting. Start runs, sets Health_atm to 100, then Update runs and sees Health_atm > 0 so calls Restart(). Which repeats the cycle on the first frame after the scene is reloaded, and again, and again…

1 Like