Home > Software design >  Unity claims that I don't have an object reference [closed]
Unity claims that I don't have an object reference [closed]

Time:09-16

it gives the error for the line 14 and 26. I don't get it I just created the object at the line 8

First one is the code that runs the healthpoint obj and second one is the code that claims I don't have an object reference.

using UnityEngine;

public class HealthPoint
{
    public float max;
    public float min = 0;
    public float current;
}

/

using UnityEngine;

    public class Obstacle : MonoBehaviour
    {
        [SerializeField] private float MaxHp = 100;
        [SerializeField] private float MinHp = 0;
    
        private HealthPoint hp;
    
        SpriteRenderer spriteRenderer;
    
        private void Start()
        {
            hp.max = MaxHp;
            hp.min = MinHp;
            hp.current = MaxHp;
        }
    
        public void GotHit(float HitPoint)
        {
            hp.current -= 20;
        }
    
        private void Update()
        {
            float color = (int)(hp.current / 100);
            spriteRenderer.color = new Color(color, color, color);
        }
    }

I was declaring it but not initializing it. I needed to assign an object to the declared var.

    using UnityEngine;

public class Obstacle : MonoBehaviour
{
    [SerializeField] private float MaxHp = 100;
    [SerializeField] private float MinHp = 0;

    private HealthPoint hp;
    private SpriteRenderer spriteRenderer;

    private void Start()
    {
        spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
        hp = new HealthPoint();

        hp.max = MaxHp;
        hp.min = MinHp;
        hp.current = MaxHp;
    }

    public void GotHit(float HitPoint)
    {
        hp.current -= HitPoint;
    }

    private void Update() //health decrees will have an another effect
    {
        float color = hp.current / 100;
        spriteRenderer.color = new Color(color, color, color);
    }
}

CodePudding user response:

The following issue here:

You are getting a null reference because you have not created an instance of the object yet. Adding either HealthPoint hp = new HealthPoint(); in the first declaration, or in the start function such as

public void Start(){
hp = new HealthPoint();
...
}

Will fix your error. However, I think you are using the start function as a constructor, which may work but is at least odd to do.

A better convention would be to edit your HealthPoint class and add the constructor there. For example

public HealthPoint(float maxHp, minHp, currentHp) {

this.max = maxHp;
this.min = minHp;
this.current = currentHp;

}

At this point you can also remove the Monobehaviour extension from the class unless you need it later

public class HealthPoint : Monobehaviour 

->

public class HealthPoint

Now once you have the constructor you can just do this in the Start() function

HealthPoint hp = new HealthPoint(MaxHp, MinHp, MaxHp); 
// passing maxhp as the current hp because I am assuming you start with max hp

CodePudding user response:

replace

private HealthPoint hp;

with

private HealthPoint hp= new HealthPoint();
  • Related