Search Unity

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

Discussion in 'Getting Started' started by I_Really_Care, Jul 24, 2020.

  1. I_Really_Care

    I_Really_Care

    Joined:
    Jun 13, 2020
    Posts:
    3
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Health : MonoBehaviour
    7. {
    8.  
    9.     public Text Health_bar;
    10.  
    11.     private int Health_atm = 0;
    12.  
    13.     public PlayerMovement movement;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         Health_atm = 100;
    19.         Health_bar.text = "Health:" + Health_atm;
    20.  
    21.     }
    22.  
    23.     public GameManager gameManager;
    24.  
    25.         private void Update()
    26.         {
    27.             if(Health_atm == 0 || Health_atm > 0){
    28.                movement.enabled = false;
    29.  
    30.                FindObjectOfType<GameManager>().Restart();
    31.             }
    32.         }
    33.              public void OnCollisionEnter(Collision collision) {
    34.              
    35.                     if(collision.gameObject.name == "Enemy"){
    36.  
    37.                         Health_atm = Health_atm  - 50;
    38.                         Health_bar = "Health:" + Health_atm.ToString();
    39.                     }
    40.         }
    41.  
    42. }
    43.  
    and the GameManager code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.  
    9.    public void Restart(){
    10.  
    11.        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    12.  
    13.    }
    14. }
    15.  
    Photo:https://imgur.com/gallery/Qybgbu3 wont let me post image so heres the link.

    any help is appreciated
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
    I_Really_Care likes this.
  3. basnijhof01

    basnijhof01

    Joined:
    May 8, 2020
    Posts:
    3
    The first if statement is incorrect. It should check for if(Health_atm <= 0)
     
    I_Really_Care likes this.
  4. I_Really_Care

    I_Really_Care

    Joined:
    Jun 13, 2020
    Posts:
    3
    in the heiarchy the scenes are duplicated and say not loaded and thanks for pointing the health thing out ill fix it
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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...
     
    I_Really_Care likes this.