0

NullReferenceException: Object reference not set to an instance of an object(cs:17) on the exact line my SetSize(health) line is and I but this on a script I got from a youtube(which wasn't working either)and I have been trying to make an HP bar/health and damage system for hours, even the youtube tutorials haven't been working. If you could share with me your damage and HP systems(preferably in scripts that I could copy and paste), that would be great, but if you can't then please help me with fixing this. I'm trying to build a horror game (first person) so a health bar isn't necessary, just a working hp system(the collisions and hitboxes I can handle), it's just the damage processing and HP system that gets me. Thanks in advance!

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using CodeMonkey.Utils;

        public class GameHandler : MonoBehaviour {

        [SerializeField] private HealthBar healthBar;

        private void Start () {
            FunctionPeriodic.Create(() =>
            {
                float health = 1f;
                if (health > .01f)
                {
                    health -= .01f;
                    healthBar.SetSize(health);

                    if (health < .3f)
                    {
                        if ((int)(health * 100f) % 3 == 0)
                        {
                            healthBar.SetColor(Color.white);
                        }
                        else
                        {
                            healthBar.SetColor(Color.red);
                        }
                    }
                }
                else
                {
                    health = 1f;
                    healthBar.SetColor(Color.red);
                }
            }, .05f);
        }
        }
derHugo
  • 83,094
  • 9
  • 75
  • 115

2 Answers2

0

I think you forgot to initialize or set your healthBar object before calling healthBar.SetSize(health);

Leppin
  • 241
  • 1
  • 7
-1

This is a shot in the dark taking that I do not know how you defined the HealthBar class. Is it possible you have not defined a default constructor that basically can 1) create a new object.

also the previous comment is correct where you have to define a var m_HealthBar = new HealthBar(); in the start or something equivalent before using setSize. as there is a difference between Declaration and allocation.

yes, with private HealthBar healthBar; you did declare an object but there was nothing allocated to it. so it is null.

The above should lead you to a warning as monoBehivior does not like being re-instantiated again and again. It is suggested that you write your healthBar class without monobehivior using the UI or GUI statements.

  • You may not use `new` on `MonoBehaviour` derived types so if this is the case which is most probable in Unity this might be very misleading – derHugo Mar 31 '21 at 14:47
  • You may not or you should not? cause again this is a NullReferenceException There is nothing the object is pointing to. yes, using new on Monobhivior derived classes, not a good idea. It will give a warning. from there the dev can work his way to learn that. HealthBar should be non monobehivior class :P. But you're right ill fix my answer to be more direct – Jaibeer Dugal Mar 31 '21 at 15:12
  • It is a `[SerializeField]` so this is supposed to be assigned via the Unity Inspector ... Now if this **would** be a "normal" class that is not somehow derived from `UnityEngine.Object` it would be Initialized directly by the deserializer ... so it can only be a `ScriptableObject` or some `Component` (or `MonoBehavior`) for both using `new` is simply wrong ;) – derHugo Mar 31 '21 at 20:13
  • maybe, you're right you do seem to have more experience with the topic. Felt bad I got downvoted. Do mention a answer that will work better. my apologies – Jaibeer Dugal Apr 02 '21 at 06:46