title: C# Generics / Unity3d - how to supply a GameObject constraint?
body: <p>I'm just learning to use C# Generics and Unity has thrown me a curve ball. I have this method into which I intend to be able to pass either a list or an array and return an item of a variable object type (could be GameObject, ParticleSystem etc depending on the array contents):</p>
  
  <pre><code>private T RetrieveItem&lt;T&gt;(IList&lt;T&gt; list) where T : Component
  {
  
      for (int i = 0; i &lt; list.Count; i++)
      {
  
          if (!list[i].GetComponent&lt;GameObject&gt;().activeInHierarchy)
          {
              return list[i];
          }
      }
  
      return list[0];
  }
  </code></pre>
  
  <p>This compiles fine, but when I try to use it:</p>
  
  <pre><code>GameObject obj = RetrieveItem&lt;GameObject&gt;(myListOrArray);
  </code></pre>
  
  <p>I get the error "The type 'UnityEngine.GameObject' cannot be used as type parameter 'T' in the generic type or method 'J_ObjectPool.RetrieveItem(IList)'. There is no implicit reference conversion from 'UnityEngine.GameObject' to 'UnityEngine.Component'."</p>
  
  <p>I've tried using Monobehaviour as the constraint and get the same error (can't cast GameObject to Monobehaviour) and I've tried using UnityEngine.Object but get the same error and additionally I can't use the GetComponent method. Finally I've tried using GameObject as the constraint but the compiler complains that "GameObject is not a valid constraint".</p>
  
  <p>Any ideas how I could get this to work? Thanks.</p>