-1

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;
        }
    }
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
wuta
  • 1
  • 1
  • Welcome to StackOverflow and Unity! What you need here is to learn some basic debugging techniques. This is one of those ones that you will eventually learn to fix or prevent almost in your sleep. (When you are actually asleep is generally the only time when these errors occur) But check the answers in https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it as tey provide tips on general debugging practises. – Chris Schaller Apr 12 '23 at 15:10

1 Answers1

0

By looking at code, my only guess is that you don't have any instances of GameManager in your scene.

starikcetin
  • 1,391
  • 1
  • 16
  • 24
  • Sorry Im new, and I have no idea how that works. Didn't even know that is needed to have it run. How would I solve this – wuta Mar 16 '23 at 21:06
  • @wuta You need to add a `GameManager` component to a `GameObject` in your scene. – starikcetin Mar 17 '23 at 08:53