Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Healthbar and score not working.

Discussion in 'Scripting' started by JimJPlaysYT, Jun 2, 2021.

  1. JimJPlaysYT

    JimJPlaysYT

    Joined:
    Jan 16, 2021
    Posts:
    68
    When I press play, I can respawn but the health bar doesn't go down and the score only goes up once. I get no errors in the console. As well as that I can only respawn when the corners hit the obstacles.
    The scripts:

    Score:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.  
    9.     public Transform player;
    10.     public Text scoreText;
    11.  
    12.  
    13.     void Update()
    14.     {
    15.         scoreText.text = player.position.x.ToString("0");
    16.     }
    17. }
    LevelManager:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class LevelManager : MonoBehaviour
    7. {
    8.     public static LevelManager instance;
    9.  
    10.     public GameObject playerPrefab;
    11.     public Transform respawnPoint;
    12.  
    13.     public CinemachineVirtualCameraBase cam;
    14.  
    15.     private void Awake()
    16.     {
    17.         instance = this;
    18.     }
    19.  
    20.     public void Respawn()
    21.     {
    22.         Debug.Log($"respawnPoint = {respawnPoint}", this);
    23.         GameObject player = Instantiate(playerPrefab, respawnPoint.position, Quaternion.identity);
    24.         cam.Follow = player.transform;
    25.     }
    26. }
    HealthBar:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthBar : MonoBehaviour
    7. {
    8.  
    9.     public Slider slider;
    10.     public Gradient gradient;
    11.     public Image fill;
    12.  
    13.     public void SetMaxHealth(int health)
    14.     {
    15.         slider.maxValue = health;
    16.         slider.value = health;
    17.  
    18.         fill.color = gradient.Evaluate(1f);
    19.     }
    20.  
    21.     public void SetHealth(int health)
    22.     {
    23.         slider.value = health;
    24.  
    25.         fill.color = gradient.Evaluate(slider.normalizedValue);
    26.     }
    27.  
    28. }
    PlayerHealth:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerHealth : MonoBehaviour
    6. {
    7.  
    8.     public int maxHealth = 3;
    9.     public int currentHealth;
    10.  
    11.     public HealthBar healthBar;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         currentHealth = maxHealth;
    17.         healthBar.SetMaxHealth(maxHealth);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     private void OnCollisionEnter2D(Collision2D collision)
    22.     {
    23.         Debug.Log(collision.gameObject.tag, collision.gameObject);
    24.  
    25.         if (collision.gameObject.CompareTag("Enemy"))
    26.         {
    27.             TakeDamage(1);
    28.         }
    29.     }
    30.  
    31.     void TakeDamage(int damage)
    32.     {
    33.         currentHealth -= damage;
    34.  
    35.         healthBar.SetHealth(currentHealth);
    36.  
    37.         if(currentHealth <= 0)
    38.         {
    39.             Destroy(gameObject);
    40.             LevelManager.instance.Respawn();
    41.         }
    42.     }
    43. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,015
    Time to find out what is going on then!

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. JimJPlaysYT

    JimJPlaysYT

    Joined:
    Jan 16, 2021
    Posts:
    68
    I'm kind of new to unity so I was just wondering if this right?

    Code (CSharp):
    1. public void SetMaxHealth(int health)
    2.     {
    3.         Debug.Log("Healthbar works");
    4.         slider.maxValue = health;
    5.         slider.value = health;
    6.  
    7.         fill.color = gradient.Evaluate(1f);
    8.     }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,015
    Assuming slider and fill is properly set out, it certainly could be.

    For such a tiny piece of code you should not cut it out and paste it here on the forum.

    Instead, cut it out and paste it in a little file called MySliderFillExperiment and make a scene and test just that one thing right in Unity, with NOTHING else of your game around onscreen. Just test until you have confidence you understand what it is doing, since slider and fill are black boxes to us.

    In less time than it takes to post to the forum you could have a definitive answer right in your project!