Get Generic Type of Component

I am trying to make an extension method for GameObject which will add a Component to that GameObject.

    public static void AddUnknownComponent<T>(this GameObject gameObject){
        gameObject.AddComponent<typeof(T)>();
    }

However, “Only assignment call increment decrement, await, and new object expressions can be used as a statement”. Any ideas on how I can get this method to work?

(I am aware of GameObject.AddComponent()…that is not what I am trying to do. Just wanted to keep out any unnecessary details from question.)

Get rid of the typeof :wink:

And if you are using it to call AddComponent, make it so that it only accepts types that are or that inherrit from component (By adding: where T : [YourTypeHere])

Like so:

public static void AddUnknownComponent<T>(this GameObject gameObject) where T : Component
    {
         gameObject.AddComponent<T>();
    }