title: Unity 2D Error Nullreferenceexception: Object Reference Not Set To An Instance Of An Object C#
body: <p>Hello I am new to coding in unity and i was trying to make the health bar go down when the player hits an object. When i use a <code>debug.log</code> it prints what i want it to print when it collides with the object however when i try to make the health go down when it hits the object it gives me this error</p>
  
  <pre><code>NullReferenceException: 
  Object reference not set to an instance of an object
  DamagePlayer.OnCollisionEnter2D (UnityEngine.Collision2D collision)
  (at Assets/Scripts/DamagePlayer.cs:30)
  </code></pre>
  
  <p>here is my code.</p>
  
  <p>My <code>Damage</code> class</p>
  
  <pre class="lang-c# prettyprint-override"><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class DamagePlayer : MonoBehaviour
  {
      public BarScript bar;
      public int playerHealth = 100;
      int damage = 10;
  
      // Start is called before the first frame update
      void Start()
      {
          print(playerHealth);
      }
  
      // Update is called once per frame
      void Update()
      {
  
      }
  
      private void OnCollisionEnter2D(Collision2D collision)
      {
  
         if(collision.collider.tag =="enemy")
          {
  
              Debug.Log("enemy");
              bar.math(damage);
          }
      }
  }
  </code></pre>
  
  <p>Health <code>Bar</code> class</p>
  
  <pre class="lang-c# prettyprint-override"><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class BarScript : MonoBehaviour
  {
      private float fillAmount;
  
      [SerializeField]
      private Image content;
  
  
      // Start is called before the first frame update
      void Start()
      {
          fillAmount = 1f;
  
      }
  
      // Update is called once per frame
      void Update()
      {
  
          content.fillAmount = fillAmount;
      }
  
      public float math(float value)
      {
          return fillAmount =(value / 100);
      }
  
  }
  </code></pre>
  
  <p>My <code>Player</code> class</p>
  
  <pre class="lang-c# prettyprint-override"><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Player : MonoBehaviour
  {
      private Rigidbody2D rb;
  
      [SerializeField]
      private float speed = 300f;
      private float jump = 400f;
      // Start is called before the first frame update
      void Start()
      {
          rb = GetComponent&lt;Rigidbody2D&gt;();
      }
  
      // Update is called once per frame
      void Update()
      {
          Movement();
      }
  
      void Movement()
      {
          if (Input.GetKeyDown("d")){
              rb.velocity = new Vector2(speed * Time.fixedDeltaTime, rb.velocity.y);
          }else if (Input.GetKeyDown("a"))
          {
              rb.velocity = new Vector2(-speed * Time.fixedDeltaTime, rb.velocity.y);
          }else if (Input.GetKeyDown("space"))
          {
              rb.velocity = new Vector2(rb.velocity.x, jump * Time.fixedDeltaTime);
          }
      }
  
  }
  </code></pre>