UnityEngine.BoxCollider != typeof(UnityEngine.BoxCollider) Why?

C# Code:

typeof(UnityEngine.BoxCollider) ==> print:UnityEngine.BoxCollider

BoxCollider[] hinges = FindObjectsOfType(typeof(UnityEngine.BoxCollider)) as BoxCollider[]; ==>is right


BoxCollider[] hinges = FindObjectsOfType(UnityEngine.BoxCollider) as BoxCollider[]; ==>this is wrong???Why

** error CS0119: Expression denotes a `type', where a`variable', `value' or`method group' was expected error CS1502: The best overloaded method match for `UnityEngine.Object.FindObjectsOfType(System.Type)' has some invalid arguments

error CS1503: Argument `#1' cannot convert`object' expression to type `System.Type' **

UnityEngine.BoxCollider!=typeof(UnityEngine.BoxCollider) ???? why

It's just the way the language works. FindObjectsOfType() takes as an argument an object of type System.Type. BoxCollider is a different type, so it can't be used as the argument.

I'm guessing what you're looking is a generic version of the function, e.g.:


Unfortunately though, there is no generic version of FindObjectsOfType (at least not AFAIK).

Like Jesse said there is no build in generic version of this function(s), but you can create your own if you really want ;)

public class MyTools 
    public static T FindObjectOfType<T>() where T : UnityEngine.Object
        return (T)UnityEngine.Object.FindObjectOfType(typeof(T));

    public static T[] FindObjectsOfType<T>() where T : UnityEngine.Object
        return (T[])UnityEngine.Object.FindObjectsOfType(typeof(T));

You can use them like all the other generic functions:

BoxCollider[] C = MyTools.FindObjectsOfType<BoxCollider>();


Just some more information on System.Type:

System.Type is the base class for reflection. Here is a short article about reflection:


System.Type is a special class that is used to describe any other classes or types. The typeof() operator returns the Type instance that describes the type of your argument.

If you use Visual C# thanks to Intellisense you can see all members of the Type class.

To get the Type instance of a certain type just use:

System.Type type = typeof(BoxCollider);

Now you can use the type to get a list of all members or functions that are included in the BoxCollider class, or get the name of the class as string.

I'll hope that explains a bit what System.Type is and hopefully you get the difference between BoxCollider and typeof(BoxCollider).