title: Detecting an object with Raycast2D
body: <p>I'm working on simple strategy game mechanics. I have a Barracks prefab. When I add the Barracks on the scene and click on the Barracks, I receive an <code>NullReferenceException</code> error:</p>
  
  <blockquote>
    <p>NullReferenceException: Object reference not set to an instance of an object PlacementController.Update () (at Assets/Scripts/PlacementController.cs:64)</p>
  </blockquote>
  
  <p>The error is received when I try to reach collider name of the Barracks using Raycast2D.</p>
  
  <p>Barracks prefab has a Box Collider2D collider(trigger is checked) and its tag is "Building" and its layer is "Buildings". It has a rigidbody2D component and it is a kinematic rigidbody. </p>
  
  <p>I can not figure out this problem. Please help me.</p>
  
  <p>Thanks for your time.</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class PlacementController : MonoBehaviour
  {
      private Buildings buildings;
      private Transform currentBuilding;
      private bool _hasPlaced;
      public LayerMask BuildingsMask;
      public void SelectBuilding(GameObject g)
      {
          _hasPlaced = false;
          currentBuilding = ((GameObject)Instantiate(g)).transform;
          buildings = currentBuilding.GetComponent&lt;Buildings&gt;();
      }
  
  bool CheckPosition()
  {
      if (buildings.CollidersList.Count &gt; 0)
      {
          return false;
      }
      return true;
  }
  
  // Update is called once per frame
  void Update () {
  
  
      Vector3 m = Input.mousePosition;
      m = new Vector3(m.x, m.y, transform.position.z);
      Vector3 p = GetComponent&lt;Camera&gt;().ScreenToWorldPoint(m);
  
  
      if (currentBuilding != null &amp;&amp; !_hasPlaced)
      {
  
          currentBuilding.position = new Vector3(p.x,p.y,0);
  
          if (Input.GetMouseButtonDown(0))
          {
              if (CheckPosition())
              {
                  _hasPlaced = true;
              }
          }
      }
      else
      {
          if (Input.GetMouseButtonDown(0))
          {
              RaycastHit2D hit = new RaycastHit2D();
              Ray2D ray2D = new Ray2D(new Vector2(p.x,p.y), Vector3.down );
              //Ray2D ray = new Ray(transform.position,new Vector3(p.x,p.y,p.z));
              if (Physics2D.Raycast(new Vector2(p.x,p.y),Vector3.down,5.0f,BuildingsMask) )
              {
                  Debug.Log(hit.collider.name); //error
              }
          }
      }
  
  }
  </code></pre>
  
  <p>------------------ I am sharing answer, and thanks for your help  --------------------</p>
  
  <pre><code> if (Input.GetMouseButtonDown(0) &amp;&amp; !EventSystem.current.IsPointerOverGameObject())
          {
              RaycastHit2D hit = new RaycastHit2D();
              Ray2D ray2D = new Ray2D(new Vector2(p.x,p.y), Vector3.down );
              hit = Physics2D.Raycast(new Vector2(p.x, p.y), Vector3.forward, 5.0f, BuildingsMask);
  
                  Debug.Log(hit.collider.name);
  
          }
  </code></pre>