Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Health 0 = Respawn (Script Help)

Discussion in 'Getting Started' started by Dyjutsen, May 23, 2020.

  1. Dyjutsen

    Dyjutsen

    Joined:
    Apr 30, 2020
    Posts:
    1
    Hello,

    Just started in Unity, and currently stuck on this script. The "Player" already is getting health deducted over time, but when it reaches 0 it doesn't Respawn, as intended.
    Would appreciate any help, thank you!


    Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class PlayerHealth : MonoBehaviour
    8. {
    9.     [Header("---Player Health Parameters---")]
    10.     public int startHealth;
    11.     public int maxHealth;
    12.     public int minHealth;
    13.     [Header("---Health UI Slider---")]
    14.     public GameObject _healthSlider;
    15.     private Slider healthSlider;
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start()
    20.     {
    21.         healthSlider = _healthSlider.GetComponent<Slider>();
    22.         healthSlider.maxValue = maxHealth;
    23.         healthSlider.minValue = minHealth;
    24.         healthSlider.value = startHealth;
    25.     }
    26.     public void ChangeHealth(int changeAmount)
    27.     {
    28.         healthSlider.value += changeAmount;
    29.         if (healthSlider.value < 0)
    30.         {
    31.             KillPlayer();
    32.         }
    33.         if (healthSlider.value > healthSlider.maxValue)
    34.             healthSlider.value = healthSlider.maxValue;
    35.         if (healthSlider.maxValue < 1)
    36.             healthSlider.value = 1;
    37.     }
    38.     private void KillPlayer()
    39.     {
    40.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    41.         FindObjectOfType<AudioManager>().Play("PlayerDeath");
    42.         }
    43.     }
     
  2. theLameBrain

    theLameBrain

    Joined:
    Wednesday
    Posts:
    3
    Looking at your code for ChangeHealth, I do not see what this is doing:
    Code (CSharp):
    1.         if (healthSlider.maxValue < 1)
    2.             healthSlider.value = 1;
    If this were my code, I would start by putting something like this:
    Code (CSharp):
    1.         if (healthSlider.value < 0)
    2.         {
    3.             Debug.Log("Player Killed!");
    4.             KillPlayer();
    5.         }
    Set the players healthSlider to -100 or so and run. Look for Player Killed! in the log.
    If you see it, move the Debug.Log down to your KillPlayer function and try again.
    If you don't see it, you know that you have a bug that is preventing your healtSlider going below 0.

    Sorry this is not more helpful, but that is all I can tell from the code posted here.
     
unityunity