Я создаю изображение пользовательского интерфейса со строкой здоровья и сценарием для него, но когда мой игрок подвергается нападению, изображение панели здоровья не меняется, просто в инспекторе здоровье показывает изменение урона.Как я могу сделать так, чтобы панель здоровья изображения интерфейса изменялась при атаке игрока?Нужно ли что-то менять в скрипте?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HealthBar : MonoBehaviour
{
public static HealthBar singleton;
public Image currentHealthbar;
public Text ratioText;
public float currentHealth;
public float maxHealth = 100f;
public bool isDead = false;
public bool isGameOver = false;
public GameObject gameOverText;
private void Awake()
{
singleton = this;
}
// Start is called before the first frame update
private void Start()
{
currentHealth = maxHealth;
UpdateHealthbar();
}
// Update is called once per frame
private void UpdateHealthbar()
{
float ratio = currentHealth/ maxHealth;
currentHealthbar.rectTransform.localScale = new Vector3(ratio, 1, 1);
ratioText.text = (ratio * 100).ToString("0") + '%' ;
}
void Update()
{
if(currentHealth < 0)
{
currentHealth = 0;
}
}
public void DamagePlayer(float damage)
{
if(currentHealth > 0)
{
currentHealth -= damage;
}
else
{
Dead();
if (isGameOver && Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
UpdateHealthbar();
}
void Dead()
{
currentHealth = 0;
isDead = true;
Debug.Log("Player is dead");
gameOverText.SetActive(true);
isGameOver = true;
}
}