title: Calling function from another script attatched to another object not working
body: <p>I am trying to call a function in another c# script, that is attatched to another gameObject in my scene. I am creating an instance of the script <code>LevelChanger</code> in the <code>Grounded</code> one like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>LevelChanger levelChanger;
  </code></pre>
  
  <p>Then, in the <code>Awake()</code> function:</p>
  
  <pre class="lang-cs prettyprint-override"><code>GameObject gameObject = new GameObject("LevelChanger");
  levelChanger = gameObject.AddComponent&lt;LevelChanger&gt;();
  </code></pre>
  
  <p>then calling like this in an <code>IEnumerator</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>levelChanger.FadeOut(true); // line 178
  </code></pre>
  
  <p>Then this is the <code>LevelChanger</code> class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Collections;
  using UnityEngine;
  
  public class LevelChanger : MonoBehaviour
  {
      public Animator animator;
  
      public void FadeOut(bool fadeIn)
      {
          animator.SetBool("Fade", true); // line 10
  
          if (fadeIn) StartCoroutine(FadeInAsWell());
      }
  
      IEnumerator FadeInAsWell()
      {
          yield return new WaitForSeconds(0.9f);
  
          animator.SetBool("Fade", false);
      }
  }
  </code></pre>
  
  <p>I've been trying to solve this problem for quite a while now, I checked everywhere (<a href="https://answers.unity.com/questions/653904/you-are-trying-to-create-a-monobehaviour-using-the-2.html" rel="nofollow noreferrer">here</a>, <a href="https://forum.unity.com/threads/you-are-trying-to-create-a-monobehaviour-using-the-new-keyword.119537/" rel="nofollow noreferrer">here</a>, <a href="https://answers.unity.com/questions/947674/you-are-trying-to-create-a-monobehaviour-using-the-5.html" rel="nofollow noreferrer">here</a>, <a href="https://stackoverflow.com/questions/40640553/why-in-unity-im-getting-the-warning-you-are-trying-to-create-a-monobehaviour-u">here</a> and on other sites). I saw that because my <code>LevelChanger</code> script is attatched to a gameobject, so it is <code>MonoBehaviour</code>, it is not possible to create an instance of the class from another script, like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>LevelChanger levelChanger = new LevelChanger();
  </code></pre>
  
  <p>or either "normally" like I was doing at first like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>LevelChanger levelChanger;
  </code></pre>
  
  <p>Then just calling its functions like </p>
  
  <pre class="lang-cs prettyprint-override"><code>levelChanger.FadeOut(true);
  </code></pre>
  
  <p>Most of the time I was getting a <code>NullReferenceException</code> at runtime (at line 176), now directly in the <code>LevelChanger</code> script (at line 10).</p>
  
  <p>At this moment, I truly have no idea how to fix this: does anybody know how?
  (I'm still a beginner).</p>
  
  <p>Thanks in advance!</p>
  
  <p><a href="https://i.stack.imgur.com/yfpKc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yfpKc.png" alt="How I attatched the animator component"></a></p>