Health Bar Image Slider Problem.

So I have a void TakeDamage(); function after watching a youtube video about how to create a health system. At the moment I’m using this function to scale down my Fill (health bar UI image which has a slider component that scales the image from right to left by whole numbers). I also have four different GameObjects, each with a Collider, and an void OnMouseDown(); function which returns { this.Collider.isTrigger = true; }.
Let’s say that I want all four GameObjects to be clicked (in my case, since the game’s a mobile game, touched) and after they’re clicked, apply void TakeDamage();
So I have created another script and attached it to an empty game object, and in it, I called all the GameObject.Colliders.isTrigger and put them in an If() statement like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MouseClick : MonoBehaviour
{

    Collider2D thisCollider;

  private void Awake()
    {
        thisCollider = this.GetComponent<Collider2D>();
        thisCollider.isTrigger = false;
    }
    private void OnMouseDown()
    {
        thisCollider.isTrigger = true;

    }
    private void OnMouseUp()
    {
        thisCollider.isTrigger = false;
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;


[code=CSharp]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthBar : MonoBehaviour
{
    public Slider slider;
    public Gradient gradient;
    public Image fill;

        public void SetMaxHealth(int health)
    {
        slider.maxValue = health;
        slider.value = health;
    }
    public void SetHealth(int health)
    {
        slider.value = health;
    }
public class Kill : MonoBehaviour
[SerializeField]
    public GameObject _enemy;

    [SerializeField]
    private GameObject _player;

    [SerializeField]
    private GameObject circleOne;

    [SerializeField]
    private GameObject circleTwo;

    [SerializeField]
    private GameObject circleThree;

    [SerializeField]
    private GameObject circleFour;


    Collider2D btnOneCollider;
    Collider2D btnTwoCollider;
    Collider2D btnThreeCollider;
    Collider2D btnFourCollider;

    public HealthBar healthBar;
    int maxHealth = 100;
    int currentHealth;

    private void Awake()
    {
        currentHealth = maxHealth;
        healthBar.SetMaxHealth(maxHealth);


        btnOneCollider = circleOne.GetComponent<Collider2D>();
        btnTwoCollider = circleTwo.GetComponent<Collider2D>();
        btnThreeCollider = circleThree.GetComponent<Collider2D>();
        btnFourCollider = circleFour.GetComponent<Collider2D>();
    }

    public void Update()
    {


        if (btnOneCollider.isTrigger && btnTwoCollider.isTrigger && btnThreeCollider.isTrigger && btnFourCollider.isTrigger && timeNeeded  == true)
        {
            TakeDamage(20);
            //Destroy(_enemy);
        }

    }

    void TakeDamage(int damage)
    {
        currentHealth -= damage;
        healthBar.SetHealth(currentHealth);
    }

But whenever I run the program, instead of reducing only 20 from health, and show currentHealth as 80, it completely reduces the currentHealth down to 0! Basically, TakeDamage Works, if I’m using an If statement with a parameter of input.KeyDown function in Update, but if I use a bool in the If statement’s parameter (like the example above), it doesn’t work as it is supposed to. Can you help me?

Time to find out what’s going on! Debugging 101 as it were.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

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?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

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 manually, looking for all the parts, where they are, what scripts are on them, etc.

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.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

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://discussions.unity.com/t/839300/3

@saji13king Where did you get this code? We saw the same code last week from another user. healthbar does not slide when damage is taken As was mentioned above, add Debug.Log statements throughout your code. Like here:

TakeDamage(20);
Debug.Log(“In update!”);