title: UNITY - Crashes whenever I try to get a variable from another script
body: <p>I have this simulation where the &quot;Hunter&quot; scans the area with the scannereyes object inside the hunter for an object called &quot;Apple&quot; and whenever it sees the apple its supposed to hunt after it. This is the same with other hunters from the same prefab as there are more hunters on the same platform.</p>
  <p>The scanner eyes is supposed to send the data of the apple's position so the hunter can run for it. Ive tried putting the found apple object into static but that would resolve into every hunter hunting for the same apple so I tried instead doing this method:</p>
  <pre><code>Target = GetComponent&lt;ScannerEyes&gt;().foundobject.transform;
  </code></pre>
  <p>And instead of giving the Hunter the data it needed the whole engine just crashes whenever it tries to compile the C sharp code.</p>
  <p>Yes everything is well updated and so is my computer. I have a strong enough computer to compile.</p>
  <pre><code>//In the Hunter CS file
   
  void Update()
      {
          if (reproducerate &lt; 1)
          {
              if (GameObject.FindGameObjectsWithTag(&quot;Food&quot;) != null ) //All apple objects have the tag &quot;Food&quot;
              {
               
                  Target = GetComponent&lt;ScannerEyes&gt;().foundobject.transform;
                  directiontolookat = (Target.position - transform.position).normalized;
               
   
                  if ((transform.position - Target.position).magnitude &gt; Distans)
                  {
                      transform.Translate(directiontolookat * Time.deltaTime * Speed);
                  }
              }
          }
      }
  
  public void OnCollisionEnter(Collision collision)
      {
          if (collision.gameObject.tag == &quot;Food&quot;)
          {
              reproducerate += 1;
              Destroy(collision.gameObject);
              Debug.Log(reproducerate);
          }
      }
  </code></pre>
  <pre><code>//In the ScannerEyes CS file:
   
   
      public bool found = false;
      public GameObject foundobject;
   
      void Update()
      {
  //This is only for the scanning sphere to grow overtime cuz thats how scanners work
          if (currentsize != maxsize &amp;&amp; !found) {
              transform.localScale = new Vector3(currentsize, currentsize, currentsize);
              currentsize += scanningspeed;
          }
   
          if (found)
          {
              transform.localScale = new Vector3(0f, 0f, 0f);
          }
      }
   
      private void OnTriggerEnter(Collider other)
      {
          if (other.gameObject.tag == &quot;Food&quot;)
          {
              Debug.Log(&quot;WE FOUND IT&quot;);
              foundobject = other.gameObject;
              Debug.Log(&quot;found at&quot; + foundobject.transform.position);
              found = true;
          }
      }
  </code></pre>
  <pre><code>//In the Food behaviour CS file (This is just to make it spawn in the world between -4, 4)
   
      private GameObject foodobject;
      public GameObject Foodprefab;
      private float Xspawncoords;
      private float Zspawncoords;
     
      void Start()
      {
          Debug.Log(&quot;Food is working&quot;);
          for (int i = 0; i &lt; 2; i++){
              Xspawncoords = Random.Range(-4, 4);
              Zspawncoords = Random.Range(-4, 4);
              Debug.Log(Xspawncoords + &quot; and &quot; + Zspawncoords);
              foodobject = Instantiate(Foodprefab,
                  new Vector3(Xspawncoords, 1.4f, Zspawncoords),
                  Quaternion.identity);
              foodobject.name = &quot;AppleObject&quot;;
          }
      }
   
  
  </code></pre>