how to approach connecting different boss parts(gameobjects) to one health bar that calculate damage when a part gets destroyed?
hello! I am currently working on a space shooter in unity 2D, right now I am working on the boss stage .. the boss in my game is made up of different parts each has their own attack or a job , I am trying to connect all of these parts to one health bar (slider) and make the HP manager detect when a part is destroyed to apply the damage.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HPManagerBoss : MonoBehaviour
{
public int maxHealth = 50;
public int currentHealth;
public Slider healthBar;
public GameObject deathAnimation;
public float waitForIT = 1;
public float cooldown = 1;
public static bool win;
void Start()
{
currentHealth = maxHealth;
win = false;
}
void Update()
{
healthBar.value = currentHealth;
if (currentHealth <= 0)
{
win = true;
cooldown -= Time.deltaTime;
Instantiate(deathAnimation, transform.position, transform.rotation);
FindObjectOfType<AudioManager>().Play("PlayerExplosion");
if (cooldown <= 0)
{
Destroy(gameObject);
cooldown = waitForIT;
SceneManager.LoadScene("WinScreen");
}
}
}
private void OnTriggerEnter2D()
{
currentHealth -= 5;
FindObjectOfType<AudioManager>().Play("WindHitWithEarth");
}
//public void partDestroyed (int damaged)
//{
// currentHealth -= damaged;
//}
}
I don't know how to do this since I tried making it when the part gets destroyed it reduces the currentHealth which I did in an IF statement and because of that it keeps checking and doing the damage so I scrapped that idea , then I tried to access the boss's health from the part's script to apply the damage but that didn't work either .. so if anyone has an idea or some info about how to do it, I would really appreciate it.
Thanks in advance