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?