Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by noflyzone 3 · Feb 08, 2011 at 06:13 AM · cs0119cs1503typeof

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 avariable', value' ormethod 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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&lt;T&gt;() 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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image noflyzone 3 · Feb 09, 2011 at 01:44 AM 0
Share

System.Type type = typeof(BoxCollider);

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

UnityEngine.BoxCollider is Object type?

avatar image Bunny83 · Feb 09, 2011 at 02:45 AM 0
Share

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?

avatar image
1

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).

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image noflyzone 3 · Feb 08, 2011 at 06:39 AM 0
Share

FindObjectsOfType()...

error CS1501: No overload for method FindObjectsOfType' takes 0' arguments

avatar image Jesse Anders · Feb 08, 2011 at 07:19 AM 1
Share

Right, as I said, there is no generic version of FindObjectsOfType().

avatar image noflyzone 3 · Feb 08, 2011 at 03:11 PM 0
Share

not about generic version . only about System.Type

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges