title: I am trying to create a health bar in unity using c# and IDK where I messed up the code, this is my handling script and it keeps saying:NullException
body: <p>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!</p>
  <pre><code>    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(() =&gt;
              {
                  float health = 1f;
                  if (health &gt; .01f)
                  {
                      health -= .01f;
                      healthBar.SetSize(health);
  
                      if (health &lt; .3f)
                      {
                          if ((int)(health * 100f) % 3 == 0)
                          {
                              healthBar.SetColor(Color.white);
                          }
                          else
                          {
                              healthBar.SetColor(Color.red);
                          }
                      }
                  }
                  else
                  {
                      health = 1f;
                      healthBar.SetColor(Color.red);
                  }
              }, .05f);
          }
          }
  </code></pre>