Search Unity

Cannot cast from source type to destination type

Discussion in 'Scripting' started by herpderpy, Jan 21, 2012.

  1. herpderpy

    herpderpy

    Joined:
    Mar 9, 2010
    Posts:
    477
    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:

    Code (csharp):
    1. foreach(Inventory.InventoryItem i in currentInventory.inventoryItems)
    2.         {
    3.             (IT THROWS THE ERROR HERE) ItemLibrary.ItemLibraryItem item = sessionLibrary.getItemByID(i.myID);
    4.            
    5.             //GUI.Button(new Rect(x, y, 40, 40), sessionLibrary.getTextureById(item.textureIndex));
    6.         }
    and this is the getTextureById function:

    Code (csharp):
    1. public ItemLibraryItem getItemByID(int id)
    2.     {
    3.         foreach(ItemLibraryItem item in libraryItems)
    4.         {
    5.             if(item.myID == id)
    6.             {
    7.                 return item;   
    8.             }
    9.         }
    10.         return new ItemLibraryItem("null", "null", 0, 0, 0, 0);
    11.     }
    Could somebody give me some pointers on what might be happening, or how I could get rid of this damn error?

    cheers :)
     
  2. herpderpy

    herpderpy

    Joined:
    Mar 9, 2010
    Posts:
    477
    Edit: Also please note that they are separate classes, and that the first piece of code is calling it from a different class.
     
  3. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    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.
     
    Last edited: Jan 21, 2012
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You don't use #pragma strict with C#, which is always strict. It's for Unityscript only.

    --Eric
     
  5. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Why are you using ItemLibrary.ItemLibraryItem? It is an inner class?
     
  6. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    There is nothing wrong with accessing inner classes if they are marked public.
     
  7. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    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 :)
     
    Last edited: Jan 21, 2012