我在Unity中写了健康,并决定附上一条HP,它给出了错误:Assets\Interfaces\HealthBar.cs(42,9):错误CS 0019:运算符“+=”不能应用于类型为“float”和“method group”的操作数Assets\Interfaces\HealthBar.cs(47,9):错误CS 0019:运算符“-=”不能应用于类型为“float”和“方法组”的操作数Assets\Interfaces\HealthBar.cs(25,22):错误CS1503:论据一:无法从'float'转换为'int'`以下是健康条形码:
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;
}
}
以及健康所在的代码本身
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;
}
}
}
}
我预计,地带将带走_健康从收到的损害和一些不工作
1条答案
按热度按时间wsxa1bj11#
原因很明显,您正在尝试将健康设置事件添加到属性中,这不仅会产生错误,而且根本没有必要。
我的建议是完全删除上面的行,而是在
SetHealth
下面添加下面的行,直接使用UI设置角色的健康状况。