How to check if a game object has a component method in Unity?
<p>I am writing a method to check if a gameObject has a component.</p>

<p>Here it is:</p>

<pre><code>public static bool HasComponent &lt;T&gt;(this GameObject obj)
{
    return obj.GetComponent&lt;T&gt;() != null;
}
</code></pre>

<p>And I'm using it like this:</p>

<pre><code>void Update()
{

    if (Input.GetKey("w"))
    {
        if (gameObject.HasComponent&lt;Rigidbody&gt;())
        {
            print("Has a rigid body.");
            return;
        }

        print("Does not have rigid body.");
    }
}
</code></pre>

<p>The gameObject does NOT have a rigid body but it is still printing that it does have.</p>