Using a Template Variable as a Component


I am trying to create a generic method that will remove duplicate Components from a GameObject.

I get the following compile error. How can I fix this?

The type `T' must be convertible to `UnityEngine.Component' in order to use it as parameter `T' in the generic type or method

Heres my code:

public static bool removeDuplicateComponents<T>(GameObject go) {

	bool hasComponent  = false;
	bool hasDuplicates = false;

	foreach (T c in go.GetComponents<T>()) {  // error line
		if (!hasComponent) {
			hasComponent = true;

		hasDuplicates = true;

	return hasDuplicates;

removeDuplicateComponents(GameObject go) where T: Component

should work