NullReferenceException: Object reference not set to an instance of an object
PlayerBehaviour.PlayerTakeDmg (System.Int32 dmg) (at Assets/Scripts/PlayerBehaviour.cs:82)
PlayerBehaviour.Update () (at Assets/Scripts/PlayerBehaviour.cs:25)
I'm trying to get the health system to work with the player.
Here I use the spacebar input to test the PlayerTakeDmg()
, but as shown above it's an error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour
{
public double _atkSpeed = 1f;
public UnitHealth _playerHealth = new UnitHealth(100, 100);
void Update()
{
///TEST HEAL/DMG
if (Input.GetKeyDown("space"))
{
PlayerTakeDmg(10);
Debug.Log(GameManager.gameManager._playerHealth.Health);
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
PlayerTakeHeal(10);
Debug.Log(GameManager.gameManager._playerHealth.Health);
}
//////////////////
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "DmgObject")
{
PlayerTakeDmg(20);
Debug.Log(GameManager.gameManager._playerHealth.Health);
}
if(collision.gameObject.tag == "DmgObject")
{
PlayerTakeDmg(20);
Debug.Log(GameManager.gameManager._playerHealth.Health);
}
}
private void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.tag == "HealObject")
{
PlayerTakeHeal(10);
Debug.Log(GameManager.gameManager._playerHealth.Health);
}
if(collider.gameObject.tag == "UatkSpeedObject")
{
atkSpeedU(0.2);
}
}
//////////////////////////////////////////////////////
private void atkSpeedU(double atkSpeedAmount)
{
if (atkSpeedAmount < _atkSpeed)
{
_atkSpeed -= atkSpeedAmount;
}
}
private void PlayerTakeDmg(int dmg)
{
GameManager.gameManager._playerHealth.DmgUnit(dmg);
}
private void PlayerTakeHeal(int healing)
{
GameManager.gameManager._playerHealth.HealUnit(healing);
}
}
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);
public UnitHealth _enemy1Health = new UnitHealth(100,100);
public UnitHealth _enemy2Health = new UnitHealth(80,80);
public UnitHealth _boxHealth = 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 UnitHealth
{
int _currentHealth;
int _currentMaxHealth;
public int Health
{
get
{
return _currentHealth;
}
set
{
_currentHealth = value;
}
}
public int MaxHealth
{
get
{
return _currentMaxHealth;
}
set
{
_currentMaxHealth = value;
}
}
//constructer
public UnitHealth(int Health, int maxHealth)
{
_currentHealth = Health;
_currentMaxHealth = maxHealth;
}
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;
}
}
}