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 XartaX · Apr 01, 2014 at 02:34 PM · castsourceinvalidcastexceptiondestination

InvalidCastException: Cannot cast from source type to destination type.

This line gives me the error in the title (C#):

 Resource[] resources = (Resource[])GetComponents(typeof(Resource));

If I understood it correctly, this should put all the Resource scripts I have attached to gameObjects in the scene in the resources Array.

Resource is just a script I made that's attached to all resource-objects in the game I'm making.

This was the closest question I found, but the problem there is slightly different (Sorry if I overlooked something):

http://answers.unity3d.com/questions/496495/invalidcastexception-cannot-cast-from-source-type-2.html

Rest of the code shouldn't be necessary, but here's the method it's used in: http://pastebin.com/nsENSJbQ

Edit: sourcelink for what I'm doing: http://forum.unity3d.com/threads/9343-How-to-use-GetComponents%28%29-in-C

Edit2: As for the documentation, following it doesn't work at all, either: https://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponents.html

Even just this line on it's own throws an error: var resourcesComponents : Resource[];

Trying this:

     Resource[] rs;
     rs = GetComponents(Resource);

Throws the error "Resource is a Type, but is used as a variable", although the documentation says GetComponents accepts Types...

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

3 Replies

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

Answer by XartaX · Apr 01, 2014 at 03:33 PM

Apparantly that forum link is just wrong, although people are in agreement there. I discovered the correct way to use it:

 GetComponents<Resource>()
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 perchik · Apr 01, 2014 at 03:38 PM 0
Share

Right, You'll notice that the docs are not wrong at all. There's two forms of GetComponent, one accepts a type and returns a Component[]:

 Component[] GetComponents(Type type)

the other returns a Generic typed array and takes nothing as a variable:

 T[] GetComponents();

Your answer uses the second syntax.

To make the first work, you have to cast a Component[] to Resource[] which might require you to iterate over the whole array, or do something with Linq to make it work.

Also, the C# example on the docs uses the second syntax quite clearly.

avatar image XartaX · Apr 01, 2014 at 03:42 PM 0
Share

No need to be an aggressive douche about it. I refreshed, checked if anyone else had answered. Noone had, so I posted my own. Timeline:

 17:25 - I post my answer
 ?? - You post your answer
 17:28 - your answer was approved
 17:33 - my answer was approved.

Basically, I had no idea your answer was there before I marked my own.

Edited out the doc bit.

But this place isn't for arguing, so I'm gone now.

avatar image Destran · Apr 01, 2014 at 04:06 PM 0
Share

agressive douche. lol :D

avatar image
1

Answer by frarees · Apr 01, 2014 at 02:35 PM

Try with:

 Resource[] resources = GetComponents<Resource>();
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 XartaX · Apr 01, 2014 at 02:38 PM 0
Share

Doesn't work. "The type arguments for method 'UnityEngine.Component.GetComponents()' cannot be inferred from the usage. Try specifying the type arguments explicitly".

Look at this: http://forum.unity3d.com/threads/9343-How-to-use-GetComponents%28%29-in-C

avatar image frarees · Apr 02, 2014 at 08:28 AM 0
Share

$$anonymous$$arkup language deleted the part from my answer... sorry about that :\

avatar image
1

Answer by perchik · Apr 01, 2014 at 03:28 PM

What about

 Resource[] rs = GetComponents<Resource>();
Comment
Add comment · Show 1 · 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 XartaX · Apr 01, 2014 at 03:35 PM 0
Share

Seems like we answered at the same time. Yeah that's the solution.

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"Cannot cast from source type to destination type"- instantiating 4 Answers

InvalidCastException: Cannot cast from source type to destination type. 4 Answers

Unable to cast from the source? 1 Answer

unable to cast from source to destination 0 Answers

Invalid Cast error when my format seems the same as the script reference example? 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