Cannot cast from source type to destination type

I seem to be having a problem with my code at the moment, the error is “Cannot cast from source type to destination type”. I’ve got “#pragma strict” at the top of my script, but I’m not getting any further indications.

This is the code that I’m using:

foreach(Inventory.InventoryItem i in currentInventory.inventoryItems)
		{
			(IT THROWS THE ERROR HERE) ItemLibrary.ItemLibraryItem item = sessionLibrary.getItemByID(i.myID);
			
			//GUI.Button(new Rect(x, y, 40, 40), sessionLibrary.getTextureById(item.textureIndex));
		}

and this is the getTextureById function:

public ItemLibraryItem getItemByID(int id)
	{
		foreach(ItemLibraryItem item in libraryItems)
		{
			if(item.myID == id)
			{
				return item;	
			}
		}
		return new ItemLibraryItem("null", "null", 0, 0, 0, 0);
	}

Could somebody give me some pointers on what might be happening, or how I could get rid of this damn error?

cheers :slight_smile:

Edit: Also please note that they are separate classes, and that the first piece of code is calling it from a different class.

**Edit:**nm I read too quick.

Could there be anyway there would be an ItemLibraryItem in the default namespace, are your includes all correct?

Try to call GetType() on what is returned from that function, but it’s somewhat hard to determine what could be off without knowing more. As it looks, it seems ok, but there could be odd naming, or overloading somewhere else causing it.

You don’t use #pragma strict with C#, which is always strict. It’s for Unityscript only.

–Eric

Why are you using ItemLibrary.ItemLibraryItem? It is an inner class?

There is nothing wrong with accessing inner classes if they are marked public.

I never said there was. I was asking if it was an inner class, there’s nothing in what he posted to suggest it is… if it is not then that may be his error.

EDIT: Although I guess if that type wasn’t defined he’d be getting a different error message unless both types were defined.

EDIT: Getting off topic… just post the full code :slight_smile: