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

Rest of the code shouldn’t be necessary, but here’s the method it’s used in: private void FindClosestResource() { Resource[] resources = (R -

Edit: sourcelink for what I’m doing:

Edit2: As for the documentation, following it doesn’t work at all, either: Unity - Scripting API: Component.GetComponents

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…

Try with:

Resource[] resources = GetComponents<Resource>();

What about

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

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