title: Unity C# Instantiating from prefab and casting to GameObject
body: <p>In Unity with C#, I am trying to Instantiate from a prefab and assign a script to the new object. To do this, I believe that I need to have the new object cast as a GameObject. However, the return type of Instantiate is UnityEngine.Object</p>
  
  <p>From the Unity <a href="http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html" rel="nofollow">manual</a>:</p>
  
  <pre><code>public GameObject wreck;
  ...
  void KillSelf () (
  ...
  GameObject wreckClone = (GameObject) Instantiate(wreck, transform.position, transform.rotation);
  </code></pre>
  
  <p>This, however, results in an InvalidCastException. I have seen it mentioned on this forum to try this instead:</p>
  
  <pre><code>UnityEngine.Object uo = Instantiate(...
  GameObject go = (GameObject)go;
  </code></pre>
  
  <p>This, however, results in the same exception.</p>
  
  <p>I have seen some posts suggesting trying to cast with:</p>
  
  <pre><code>...Instantiate(...) as GameObject;
  </code></pre>
  
  <p>This syntax does not, however, appear to be supported by the compiler. </p>
  
  <p>This is pretty confusing. I'm copying small bits of code out of (presumably correct) manuals and stack overflow answers, and they just don't seem to function. </p>
  
  <p>I'm not sure what information would be necessary to help solve this puzzle, so just ask me for what you need.</p>
  
  <p>So far i have tried logging the object instantiated before trying to cast it. It shows up as ThrownFood(clone)(UnityEngine.transform). I don't know why it would say transform though. I don't have anything telling it to be cast as a transform.</p>