- Home /
Enemy Health Bar Fill (Unity2D)
So I have a Enemy. I have a enemy script with health and such, and I have a script which deals damage to the enemy, I have a healthbar with fill that is a child of my enemy i wnat it to represent my enemies health so when my enemy takes damage my health bar lowers.
Enemy Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyFollow : MonoBehaviour {
public float speed;
public float ehealth;
private Transform target;
// Use this for initialization
void Start () {
EnemyHealthBar = GetComponent<Image>();
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
// Update is called once per frame
void Update () {
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
if(Vector2.Distance(transform.position, transform.position) <= 5)
{
Debug.Log("Help");
}
if(ehealth <= 0)
{
Die();
if (HealthBarScript.health < 10)
{
HealthBarScript.health += 0.5f;
}
}
}
public void Die()
{
Destroy(this.gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Destroy(this.gameObject);
HealthBarScript.health -= 1f;
}
}
}
Script that deals damage to enemy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Projectile : MonoBehaviour {
private GameObject triggeringEnemy;
private Vector2 target;
public float speed;
public float damage;
// Use this for initialization
void Start () {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
// Update is called once per frame
void Update () {
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
if(Vector2.Distance(transform.position, target) < 0.1f)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Enemy")
{
Destroy(gameObject);
triggeringEnemy = other.gameObject;
triggeringEnemy.GetComponent<EnemyFollow>().ehealth -= damage;
}
}
}
Answer by Vega4Life · Dec 09, 2018 at 06:17 PM
This should give you an idea of how to deal with it. You hit the space bar, deals damage, reduces the health bar.
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Slap this on your health bar object and link the image that has the fill
/// </summary>
public class HealthBar : MonoBehaviour
{
[SerializeField] Image healthBar;
float startingHealth = 100f;
// This property is the main theme here - as it updates it auto updates the health bar
float currentHealth;
float CurrentHealth
{
get { return currentHealth; }
set
{
currentHealth = value;
healthBar.fillAmount = currentHealth / startingHealth;
}
}
bool isDead { get { return CurrentHealth <= 0; } }
void Start()
{
CurrentHealth = startingHealth;
}
void Update()
{
// Just faking damage for testing so you can see how I do it
// Your damage will be coming from somewhere else
if (Input.GetKeyDown(KeyCode.Space))
{
if (isDead == false)
{
DealDamage(Random.Range(5f, 25f));
}
}
}
void DealDamage(float damageAmount)
{
CurrentHealth -= damageAmount;
}
}
I am quite new to Unity so I cannot undertand how I can apply this to my script?
It's for you to look it over, pick out the parts you need. $$anonymous$$ore than likely you just need the CurrentHealthProperty. Copy this out and put it in your the script that has health access and health bar fill.
// This property is the main theme here - as it updates it auto updates the health bar
float currentHealth;
float CurrentHealth
{
get { return currentHealth; }
set
{
currentHealth = value;
healthBar.fillAmount = currentHealth / startingHealth;
}
}
Not to be mean, but this is where you have to look over what I have, figure out how it works. Then retro fit it into what you are doing. That's the fun part of program$$anonymous$$g. You should have all the pieces to make your healthbar work. :)
Answer by elvish_unity · Dec 09, 2018 at 06:55 PM
I slapped on a EnemyHealthBarScript onto my EnemyHealthBars fill.
The code is like this
using UnityEngine; using UnityEngine.UI;
public class EnemyHealthBarScript : MonoBehaviour { Image EnemyHealthBar;
void Start() { EnemyHealthBar = GetComponent<Image>(); } void Update() { EnemyHealthBar.fillAmount = EnemyFollow.ehealth; } }
` But I get an error = "An object reference is required to access non-static static member". I need a little guiding.
Your answer
Follow this Question
Related Questions
Enemy won't damage player C# 1 Answer
Need Help About Health,Damage 1 Answer
Health change than object health is not decreasing ? 2 Answers