Код: Выделить всё
public class ClickController : MonoBehaviour
{
public static event Action OnClickControllerEvent; // new
private void Update()
{
if (Input.GetMouseButtonDown(0))
OnLeftClick();
}
private void OnLeftClick()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log("The cube was clicked! Notifying subscribers!");
if (OnClickControllerEvent != null) // new
OnClickControllerEvent(this);
}
}
}
Код: Выделить всё
public class HealthController : MonoBehaviour
{
[SerializeField] private int minHealth = 0;
[SerializeField] private int maxHealth = 100;
private int health = 100;
private void OnEnable() // new
{
ClickController.OnClickControllerEvent += TakeDamage;
}
private void OnDisable() // new
{
ClickController.OnClickControllerEvent -= TakeDamage;
}
private void TakeDamage(ClickController clickController) // changed
{
health -= 10;
if (health