Cannot cast from source type to destination type (98913)

Ok So I am having difficulty figuring out how to solve this issue. My program says there is nothing wrong when it is not running but when I run it I get the following error.

  • “InvalidCastException: Cannot cast from source type to destination type.
    Deck.AddToDeck (System.Collections.Generic.List`1 myDeck) (at Assets/Scripts/Cards/Deck.cs:47)
    Deck.awake (System.String deckName) (at Assets/Scripts/Cards/Deck.cs:16)
    Game.Start () (at Assets/Scripts/Game.cs:40)
    ”

     //Deck.cs
     
     public void awake(string deckName)
     	{
     		//This will be changed to take value from player + enemy current deck.
     		Load(deckName);
     		AddToDeck(readFile);
     	}
     
     	public void Load(string file)
     	{
     		readFile.Clear(); //Clears the list that will store what is read in from the file.
     		//Creates StreamReader pointing to where application data is store + '/' or '\' + filename
     		StreamReader sw = new StreamReader(Application.dataPath + System.IO.Path.DirectorySeparatorChar + file);
     
     		while(!sw.EndOfStream)
     		{
     			string line = sw.ReadLine();
     			int i;
     			if(line != null)
     			{
     				i = Int32.Parse(line);
     				readFile.Add(new CardAttributes{cardType=i});
     			}
     		}
     
     	}
     
     	public void AddToDeck(List<CardAttributes> myDeck)
     	{
     		deck.Clear();
     		foreach(CardAttributes card in myDeck)
     		{
     			switch(card.cardType)
     			{
     				case CardAttributes.CARD1:
     					**BattleChariot derivedCard = (BattleChariot) card;
     					deck.Add(derivedCard);**
     					break;
     				case CardAttributes.CARD2:
     					Bondi derivedCard2 = (Bondi) card;
     					deck.Add(derivedCard2);
     					break;
     			}
     		}
     	}
    

    //Game.cs

     public Deck deckInstance;
    
     public List<CardAttributes> playerDeck = new List<CardAttributes>();
     public List<CardAttributes> aiDeck = new List<CardAttributes>();
    
     GameObject PlayerWins;
     GameObject PlayerTurn;
     GameObject AIWins;
     GameObject AITurn;
    
     GameObject [] Buttons;
    
     string file = "Celtic.txt";
    
     enum GameState
     {
     	Begin,
     	PlayerTurn,
     	AITurn,
     	Pause,
     	PlayerWin,
     	AIWin
     };
    
     GameState m_state;
    
    
     // Use this for initialization
     void Start () 
     {
     	m_state = GameState.Begin;
     	//calls a instance of deck
     	//Deck deckInstance = (Deck)this.GetComponent(typeof(Deck));
     	**deckInstance.awake (file);**
     	PlayerWins = this.transform.Find("MessagePlayerWin").gameObject;
     	PlayerTurn = this.transform.Find("MessagePlayerTurn").gameObject;
     	AIWins = this.transform.Find("MessageAIWin").gameObject;
     	AITurn = this.transform.Find("MessageAITurn").gameObject;
     	PlayerWins.SetActive(false);
     	PlayerTurn.SetActive(false);
     	AIWins.SetActive(false);
     	AITurn.SetActive(false);
     	Buttons[0] = this.transform.Find("Button1").gameObject;
     	Buttons[1] = this.transform.Find("Button2").gameObject;
     	Buttons[2] = this.transform.Find("Button3").gameObject;
     	Buttons[3] = this.transform.Find("Button4").gameObject;
     	UpdateButtons();
    
     }
    

Line 47 of desk.cs is blank. I'm assuming you didn't include parts of it, so what line in your post is causing the error.

–

Sorry, completely forgot the line numbers would have changed...It's been a long day.

–

1 Answer

1

Keeping your code in the same lines would help identify the problem, make sure you include the code that’s relevant to the problem.

It’s not possible to see exactly what you’re trying to do but there’s no chance this can be right:

case CardAttributes.CARD1:
   BattleChariot derivedCard = (BattleChariot) card;
   deck.Add(derivedCard);
break;
case CardAttributes.CARD2:
   Bondi derivedCard2 = (Bondi) card;
   deck.Add(derivedCard2);
break;

You’re trying to cast two different classed objects to a list that probably only supports one class.

Hmmm ok, well what I am trying to do is build a list of cards, and each card is an individual class. Would it be a better idea to build a list of the ID's and then based off the ID load in the individual card class?

–

I'll have a look at this in the morning, much appreciated Nugget!

–