- Home /
UnityEngine.BoxCollider != typeof(UnityEngine.BoxCollider) Why?
C# Code:
typeof(UnityEngine.BoxCollider) ==> print:UnityEngine.BoxCollider
BoxCollider[] hinges = FindObjectsOfType(typeof(UnityEngine.BoxCollider)) as BoxCollider[]; ==>is right
But
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</p> <p>error CS1503: Argument <code>#1' cannot convert</code>object' expression to type
System.Type'
UnityEngine.BoxCollider!=typeof(UnityEngine.BoxCollider) ???? why
Answer by Bunny83 · Feb 08, 2011 at 03:19 PM
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>();
EDIT
Just some more information on System.Type:
System.Type is the base class for reflection. Here is a short article about reflection:
http://www.codeguru.com/csharp/csharp/cs_misc/reflection/article.php/c4257
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).
System.Type type = typeof(BoxCollider);
But UnityEngine.BoxCollider != typeof(UnityEngine.BoxCollider) Why?
UnityEngine.BoxCollider is Object type?
Because as i explained above typeof() returns a "instance" of type Systen.Type. You can't compare a class type with an instance reference. And System.Type is not part of the class - hierarchy of BoxCollider or any other class. System.Type is a descriptive class that is used to describe other classes. You can not pass a class to a function. Only class instances, real objects. In case of FindObjectsOfType you don't want to give this function a BoxCollider object, ins$$anonymous$$d you give the function an instance to a Type object that describes the BoxCollider class. $$anonymous$$aybe buy a C# book?
Answer by Jesse Anders · Feb 08, 2011 at 06:35 AM
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.:
...FindObjectsOfType<BoxCollider>()...
Unfortunately though, there is no generic version of FindObjectsOfType (at least not AFAIK).
FindObjectsOfType()...
error CS1501: No overload for method FindObjectsOfType' takes
0' arguments
Right, as I said, there is no generic version of FindObjectsOfType().
Your answer
Follow this Question
Related Questions
Unity Errors 1 Answer
Script Errors (CSO119, CS1503 & CS1503) 1 Answer
ScriptableWizard Problem with LoadAssetAtPath 1 Answer
Gui label errors 5 Answers
Serialize a Dictionary 1 Answer