title: C# - Generic Method to return type according parameter
body: <p>I have this generic method:</p>
  
  <pre><code>    public static T FindObject&lt;T&gt; (this GameObject gameObject, string objectName, Type type)
      {
  
         var ret = gameObject.GetComponentsInChildren(type).Where(w =&gt; w.name == objectName).First();
  
         return (T)Convert.ChangeType(ret, type);
  
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/dTapk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dTapk.png" alt="enter image description here"></a></p>
  
  <p>And I call it this way:</p>
  
  <pre><code>var myVar = UI_POINTS.FindObject("Score", typeof(Text));
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Jx05I.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jx05I.png" alt="enter image description here"></a></p>
  
  <p>But i give the following error:</p>
  
  <blockquote>
    <p>The type arguments for method
    'ExtensionMethods.FindObject(GameObject, string, Type)' cannot be
    inferred from the usage. Try specifying the type arguments
    explicitly.</p>
  </blockquote>
  
  <p>For what reason he does not understand the method call?</p>
  
  <p>Thank you in advance.</p>