'InvalidCastException: Specified cast is not valid. Unity [duplicate]

I have one main Objects and two children SpellCard and Unit

public class Objects
{
    public enum ObjectType{
        Unit,
        Spell
    }

    public Objects()
    {
        ValObjectType = ObjectType.Unit;
    }

    public Objects(Objects objects)
    {
        ValObjectType = ObjectType.Unit;
    }

    public Objects GetCopy()
    {
        return new Objects(this);
    }
}


public class Unit : Objects
{
    public int HP;

    public Unit(int hp)
    {
        ValObjectType = Objects.ObjectType.Spell;
        HP = hp;
    }

    public Unit(Unit objects) : base (objects)
    {
        ValObjectType = Objects.ObjectType.Spell;
        HP = objects.HP;
    }

    public new Unit GetCopy()
    {
        return new Unit(this);
    }
}


public class SpellCard : Objects
{
    public int Manacost;

    public SpellCard(int manacost)
    {
        ValObjectType = Objects.ObjectType.Spell;
        Manacost = manacost;
    }

    public SpellCard(SpellCard objects) : base (objects)
    {
        ValObjectType = Objects.ObjectType.Spell;
        Manacost = objects.Manacost;
    }

    public new SpellCard GetCopy()
    {
        return new SpellCard(this);
    }
}

CardController

public class CardController : MonoBehaviour
{
    public Objects Objects;
    public CardInfoScr Info;

    GameManagerScr gameManager;

    public void Init(Objects objects, bool isPlayerCard)
    {
        Objects = objects;
        Info.ShowCardInfo();
    }
    
    
    
    
    //Use spell
    public void UseSpell(CardController target)
    {

        var spellCard = (SpellCard)Objects;
        
        //Work
        Debug.Log(spellCard.Manacost);


    }
    
    
    
    
    
    
    
    
    
}

CardInfoScr

public class CardInfoScr : MonoBehaviour {

    public CardController CC;


    public void ShowCardInfo()
    {

            switch (CC.Objects.ValObjectType)
            {

                case Objects.ObjectType.Spell:
                    
                    var spellCard = (SpellCard)CC.Objects // don't work
                    Debug.Log(spellCard.Manacost);
                    
                    
                    
                    break;


                case Objects.ObjectType.Unit:

                    var unitCard = (Unit)CC.Objects // don't work
                    Debug.Log(unitCard.HP);
                    

                    
                    break;

            }
        
    }

}

I get error

InvalidCastException: Specified cast is not valid.

I checked Objects Objects = CC.Objects I didn't get an error. If they are the same then why did I get this error
Can anyone answer what is the reason and how to fix it?

Is it possible to somehow check the Object or how to convert it to Unit and Spell.



Solution 1:[1]

In Unit you have

ValObjectType = Objects.ObjectType.Spell;

.. looks like a typo to me ;)

It should be Unit there of course.

But actually your enum is completely redundant!

Simply use a type switch via pattern matching like e.g.

switch (CC.Objects)
{
    case SpellCard spellcard:
         Debug.Log(spellCard.Manacost);
        break;

    case Unit unitCard:
        Debug.Log(unitCard.HP);            
        break;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 derHugo