ScartGames
ScartGames

Reputation: 1

Health bar Unity

I wrote health in unity and decided to attach a strip of hp and it gives out errors: Assets\Interfaces\HealthBar.cs(42,9): error CS0019: Operator '+=' cannot be applied to operands of type 'float' and 'method group' Assets\Interfaces\HealthBar.cs(47,9): error CS0019: Operator '-=' cannot be applied to operands of type 'float' and 'method group' Assets\Interfaces\HealthBar.cs(25,22): error CS1503: Argument 1: cannot convert from 'float' to 'int'` Here's the health bar code:

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

public class HealthBar : MonoBehaviour
{
    public Slider slider;
    public DamageableCharacter damageableCharacter;

    private void Start()
    {
        if (slider == null)
        {
            Debug.LogError("Slider reference is not set.");
            return;
        }

        if (damageableCharacter == null)
        {
            Debug.LogError("DamageableCharacter reference is not set.");
            return;
        }

        SetMaxHealth(damageableCharacter.Health);
    }

    public void SetMaxHealth(int maxHealth)
    {
        slider.maxValue = maxHealth;
        slider.value = maxHealth;
    }

    public void SetHealth(int health)
    {
        slider.value = health;
    }

    // Using events or delegates to trigger health bar update when health changes
    private void OnEnable()
    {
        damageableCharacter.Health += SetHealth;
    }

    private void OnDisable()
    {
        damageableCharacter.Health -= SetHealth;
    }
}

and the code itself where the health is located

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class DamageableCharacter : MonoBehaviour, IDamageable
{   
    public GameObject healthText;
    public bool disableSimulation = false;

    public bool isInvincibillityEnabled = false;
    public float invincibillityTime = 0.25f;

    Animator animator;
    Rigidbody2D rb;
    Collider2D physicsCollider;

    bool isAlive = true;
    private float invincibillityTimeElapsed = 0f;

    public float Health{
        set {
            if(value < _health){
                animator.SetTrigger("hit");
                RectTransform textTransform = Instantiate(healthText).GetComponent<RectTransform>();
                textTransform.transform.position = Camera.main.WorldToScreenPoint(gameObject.transform.position);

                Canvas canvas = GameObject.FindObjectOfType<Canvas>();
                textTransform.SetParent(canvas.transform);
            }

            _health = value;
            
            if(_health <= 0) {
                animator.SetBool("isAlive", false);
                Targetable = false;
            }
        }
        get {
            return _health;
        }
    }

    public bool Targetable { 
        get {
            return _targetable;
        } set {
            _targetable = value;

            if(disableSimulation) {
                rb.simulated = false;
            }

            physicsCollider.enabled = value;
        }
    }

    public bool Invincible { 
        get {
            return _invincible;
        } set {
            _invincible = value;

            if(_invincible == true) {
                invincibillityTimeElapsed = 0f;
            }
        }
    }


    public float _health = 5f;
    bool _targetable = true;
    bool _invincible = false;
    public void Start(){
        animator = GetComponent<Animator>();
        animator.SetBool("isAlive", isAlive);
        rb = GetComponent<Rigidbody2D>();
        physicsCollider = GetComponent<Collider2D>();
    }


    public void OnHit(float damage) {
        if(!Invincible) {
            Health -= damage;

            if(isInvincibillityEnabled){
                Invincible = true;
            }

        }

        
    }

    public void OnHit(float damage, Vector2 knockback){

        if(!Invincible) {
            Health -= damage;
            rb.AddForce(knockback, ForceMode2D.Impulse);

            if(isInvincibillityEnabled){
                Invincible = true;
            }
        }

        
    }

    public void OnObjectDestroyed() {
        
        Destroy(gameObject);
    }

    public void FixedUpdate() {
        if(Invincible) {
            invincibillityTimeElapsed += Time.deltaTime;

            if(invincibillityTimeElapsed > invincibillityTime) {
                Invincible = false;
            }
        }
    }
}

I expected that the strip will take away _health from the received damage and something does not work

Upvotes: 0

Views: 265

Answers (1)

KiynL
KiynL

Reputation: 4266

The reason is clear, you are trying to add the health setting event to the property, which not only gives an error, but is simply unnecessary.

    private void OnEnable() // ????????
    {
        damageableCharacter.Health += SetHealth;
    }

    private void OnDisable() // ????????
    {
        damageableCharacter.Health -= SetHealth;
    }

My suggestion is to remove the above lines entirely and instead add the following line under SetHealth to directly set the character's health with the UI.

public void SetHealth(float health)
{
    slider.value = health;
    damageableCharacter.Health = (float) health;
}

Upvotes: 0

Related Questions