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; } } }
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(); }