- Home /
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...
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>()
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.
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.
Answer by frarees · Apr 01, 2014 at 02:35 PM
Try with:
Resource[] resources = GetComponents<Resource>();
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
$$anonymous$$arkup language deleted the part from my answer... sorry about that :\
Answer by perchik · Apr 01, 2014 at 03:28 PM
What about
Resource[] rs = GetComponents<Resource>();
Seems like we answered at the same time. Yeah that's the solution.
Your answer
Follow this Question
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