Invalid Cast Exception troubles.

Hi. So me and a group are trying to make a castle defence game in MonoGame for school, and we’re following this tutorial. However, when we’re trying to load the map, we get the following error: ‘Unable to cast object of type ‘Microsoft.Xna.Framework.Graphics.Texture2D’ to type ‘MonoGame.Extended.Tiled.TiledMapTileset’.’ And we’re all a bit stumped by it. Would love some help with this.

Can you post your stack trace? It’s hard to help with “we’re getting an exception somewhere”

I’m GUESSING it’s somewhere in content.load, but it’s really hard to say from the amount of information you’ve provided.

My bad on that one. Your guess would be correct though, it’s in the Content.Load function. Specifically, it’s the following line:
_tiledMap = Content.Load(“samplemap”);

In your content is “samplemap” a Tile Map or an Image? Based on the error it thinks it’s an image or the load is defaulting to that type.

You could try:

_tiledMap = Cotent.Load<type>("samplemap");

Where type is the type of _tiledMap

I appear to have missed that when I typed out the code, because it is specified as a TiledMap.
_tiledMap = Content.Load(“samplemap”); My bad on that one. samplemap is a .xnb file.

If you are using the MGCB editor maybe you have set your map to the incorrect type in the editor. If the file is actually a TileSet, check that it is not set to process as a “Texture - Monogame”.

(See the picture).

Monogame is defaulting to a ‘Tiled Map Processor’. I tried setting it as a Map Tileset, but that broke it. MGCB will build it correctly as a Tiled Map Processor, but then the Content.Load fails. So I don’t exactly know how to fix it.

I’ve had the same problem today. Using my own assets removed the error, so I assume the error is with the assets (possibly one of the tilesets the samplemap is made from).

Try using a different tilemap and tileset.