I set my player’s health to 100 but in the debug it says its 0 and its not affected by code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager gameManager { get; private set; }
public UnitHealth _playerHealth = new UnitHealth(100, 100);
void Awake()
{
if (gameManager != null && gameManager != this)
{
Destroy(this);
}
else
{
gameManager = this;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour
{
void Start()
{
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
PlayerTakeDmg(20);
Debug.Log(GameManager.gameManager._playerHealth.Health);
}
if (Input.GetButtonDown("Fire2"))
{
PlayerHeal(20);
Debug.Log(GameManager.gameManager._playerHealth.Health);
}
}
private void PlayerTakeDmg(int dmg)
{
GameManager.gameManager._playerHealth.DmgUnit(dmg);
}
private void PlayerHeal(int healing)
{
GameManager.gameManager._playerHealth.HealUnit(healing);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitHealth : MonoBehaviour
{
// Fields
int _currentHealth;
int _currentMaxHealth;
// Properties
public int Health
{
get
{
return _currentHealth;
}
set
{
_currentHealth = value;
}
}
// Properties
public int MaxHealth
{
get
{
return _currentMaxHealth;
}
set
{
_currentMaxHealth = value;
}
}
//Constructor
public UnitHealth(int health, int maxhealth)
{
_currentHealth = health;
_currentMaxHealth = maxhealth;
}
//methods
public void DmgUnit(int dmgAmount)
{
if (_currentHealth > 0)
{
_currentHealth -= dmgAmount;
}
}
public void HealUnit(int healAmount)
{
if (_currentHealth < _currentMaxHealth)
{
_currentHealth += healAmount;
}
if (_currentHealth > _currentMaxHealth)
{
_currentHealth = _currentMaxHealth;
}
}
}