title: Convert GameObject back to generic type <T>
body: <p>I am implementing a deep object copier for Unity.</p>
  
  <p>I found this great serialization/deserialization method here: <a href="https://stackoverflow.com/a/78612/3324388">https://stackoverflow.com/a/78612/3324388</a></p>
  
  <p>However I hit a snag with MonoBehaviour objects. If the type is a GameObject, I need to use <code>Instantiate</code> instead of the serialization. So I've add a check:</p>
  
  <pre><code>if (typeof(T) == typeof(GameObject))
  {
      GameObject clone = Instantiate(source as GameObject);
      T returnClone = clone as T;
      return returnClone;
  }
  </code></pre>
  
  <p>I am able to cast the source as a GameObject (using <code>as</code>) but when I try to do it in reverse it fails with</p>
  
  <blockquote>
    <p>The type parameter <code>T</code> cannot be used with the <code>as</code> parameter because
    it does not have a class type constraint nor a 'class' constraint.</p>
  </blockquote>
  
  <p>If I try just casting it like:</p>
  
  <pre><code>if (typeof(T) == typeof(GameObject))
  {
      GameObject clone = Instantiate(source as GameObject);
      T returnClone = (T)clone;
      return returnClone;
  }
  </code></pre>
  
  <blockquote>
    <p>Cannot convert GameObject to type <code>T</code></p>
  </blockquote>
  
  <p>I feel I'm close but I can't quite get the casting right. Do you know what I am missing to get this to work?</p>
  
  <p>If I cast the type to conform the error still persists:
  <a href="https://i.stack.imgur.com/ZRuPz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZRuPz.png" alt="enter image description here"></a></p>