title: Get image from script attached to gameobject using getComponent in Unity
body: <p>I'm trying to introduce a radar scan feature so that when a button is clicked the image on the radar is updated based on the tag of the object with a new image. I'm getting a "NullReferenceException: Object reference not set to an instance of an object" on Enemy.GetComponent line in my Radar Scan Script.
  but I have a image set?
  dot 1 which I will now show below</p>
  
  <p><a href="https://i.stack.imgur.com/uRBpB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uRBpB.jpg" alt="enter image description here"></a></p>
  
  <p>Am I accessing it wrong? Here's my RadarScan script</p>
  
  <pre><code>public class RadarScan : MonoBehaviour
  {
  GameObject Enemy;
  Image RadarImageToChange;
  
  void Start()
  {
      Enemy = GameObject.Find("Enemy");
  }
  
  public void ChangeImage(Image UpdateImage)
  {
      if (Enemy.tag == "Enemy")
      {
          RadarImageToChange = Enemy.GetComponent&lt;MakeRadarObject&gt;().image;
          UpdateImage = RadarImageToChange;
      }
  
  }
  }
  </code></pre>
  
  <p>Here's my radar Script </p>
  
  <pre><code>public class RadarObject
  {
      public Image icon { get; set; }
      public GameObject owner { get; set; }
  }
  
  public class Radar : MonoBehaviour {
  
  public Transform playerPos; //position of player
  float mapScale = 0.1f; //scale radar size
  
  public static List&lt;RadarObject&gt; radObjects = new List&lt;RadarObject&gt;();
  
  //Registers Object to the radar
  public static void RegisterRadarObject(GameObject o, Image i)
  {
      Image image = Instantiate(i);
      radObjects.Add(new RadarObject() { owner = o, icon = image }); //adds to List
  }
  
  //It loops through the list looking for the owner existing in the list, when it finds the owner is detroys the icon
  public static void RemoveRadarObject(GameObject o)
  {
      //New list for destroyed objects
      List&lt;RadarObject&gt; newList = new List&lt;RadarObject&gt;();
      for (int i = 0; i &lt; radObjects.Count; i++)
      {
          if (radObjects[i].owner == o)
          {
              Destroy(radObjects[i].icon);
              continue;
          }
           else
              newList.Add(radObjects[i]);
          }
      radObjects.RemoveRange(0, radObjects.Count);
      radObjects.AddRange(newList);
  }
  
  
  void DrawRadarDots()
  {
      //loops through the list and for each Object it gets the owners transform position and determins the difference between it's
      //position and the players position, does calculations on the angle and distance and position on a circle using polar equations.
      foreach (RadarObject ro in radObjects)
      {
          Vector3 radarPos = (ro.owner.transform.position - playerPos.position);
          float distToObject = Vector3.Distance(playerPos.position, ro.owner.transform.position) * mapScale;
          float deltay = Mathf.Atan2(radarPos.x, radarPos.z) * Mathf.Rad2Deg - 270 - playerPos.eulerAngles.y;
          radarPos.x = distToObject * Mathf.Cos(deltay * Mathf.Deg2Rad) * -1;
          radarPos.z = distToObject * Mathf.Sin(deltay * Mathf.Deg2Rad);
  
          //grabs icon of players objects and make it a child of panel and set it's postion based on radarPos.x and radarPos.z
          ro.icon.transform.SetParent(this.transform);
          ro.icon.transform.position = new Vector3(radarPos.x, radarPos.z, 0) + this.transform.position;
      }
  }
  
      //Update is called once per frame
      void Update ()
      {
          DrawRadarDots();
      }
   }
  </code></pre>
  
  <p>Here is my stack trace
  <a href="https://i.stack.imgur.com/k0dzH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k0dzH.jpg" alt="enter image description here"></a></p>