title: Unity raycast collide object and print its name on GUI
body: <pre><code>using UnityEngine;
  using System.Collections;
  public class GuardSample : MonoBehaviour 
  {
  FOV2DEyes eyes;
  FOV2DVisionCone visionCone;
  float speed = -5;
  RaycastHit hit;
  
  
  void Start() 
  {
      eyes = GetComponentInChildren&lt;FOV2DEyes&gt;();
      visionCone = GetComponentInChildren&lt;FOV2DVisionCone&gt;();
  }
  
  void FixedUpdate()
  {
      if (transform.position.x &lt; -10 || transform.position.x &gt; 10)
      {
          speed *= -1;
      }
  
      transform.position = new Vector3(transform.position.x + speed * Time.fixedDeltaTime, transform.position.y, transform.position.z);
  }
  
  bool playerInView = false;
  
  void Update()
  {
      playerInView = false;
      foreach (RaycastHit hit in eyes.hits)
      {
          if (hit.transform &amp;&amp; hit.transform.tag == "Player")
          {
              playerInView = true;
          }
      }
  
  }
  
  void OnGUI()
  {
      if (playerInView)
      {
          GUI.Box (new Rect (10, 10, 160, 60), "Title");
          GUI.Label( new Rect(10, 10, 160, 60), hit.collider.gameObject.name);
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My gaurd moves and when the player comes inside raycast, the GUI appear but name does not identify</p>
  
  <p>everything works except "hit.collider.gameObject.name"
  Unity gives error that "object reference not set to an instance of an object"</p>
  
  <p>please have a look i am new in Unity and C#</p>