- Home /
Difficulty with acessing data in a list from a custom class
I know what the issues are with what I am trying to do but I do not now how to remedy them. I am making a simple clone card game and have a list of "cards". These cards are defined in a custom class "Deck". Whenever I try to access the list to randomly select cards I get a few errors.
I have a different list that is just a list of Game Objects with the same name that works correctly. It is commented out for now. The reason I had for the new list is that I wanted to assign value to the cards rather than only list them. The errors I'm receiving are.
(64,30): error CS0029: Cannot implicitly convert type Deck' to
UnityEngine.GameObject'
(67,20): error CS1502: The best overloaded method match for System.Collections.Generic.List.Remove(Deck)' has some invalid arguments** **(67,27): error CS1503: Argument
#1' cannot convert UnityEngine.GameObject' expression to type
Deck'
Deck Class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Deck : MonoBehaviour
{
public GameObject theCard;
public int cardValue;
public string cardName;
public Deck (GameObject theCard, int cardValue, string cardName)
{
this.theCard = theCard;
this.cardValue = cardValue;
this.cardName = cardName;
}
}
GamePlay Script
public class GamePlay : MonoBehaviour {
public GameObject cur_Card;
private int cardIndex;
public Button deckButton;
private int total_Cards;
public GameObject game_Master;
private CardDeck deckScript;
private Deck dScript;
// Use this for initialization
void Start ()
{
deckButton.onClick.AddListener(DealCards);
deckScript = GetComponent<CardDeck>();
total_Cards = deckScript.cards.Count;
Debug.Log("Total Cards = " + total_Cards);
}
///////////////////THIS IS WHERE THE ERRORS ARE LOCATED/////////////////
//Button for dealing cards
private void DealCards()
{
if(cur_Card.active)
{
cur_Card.SetActive(false);
}
print("Deal a Card!");
cardIndex = Random.Range (0, total_Cards);
cur_Card = deckScript.cards[cardIndex];
Debug.Log("Current Card: " + cur_Card.name);
cur_Card.SetActive(true);
deckScript.cards.Remove(cur_Card);
total_Cards = deckScript.cards.Count;
}
}
Script that initializes the List
public class CardDeck : MonoBehaviour
{
//public List<GameObject> cards = new List<GameObject>();
public List<Deck> cards = new List<Deck>();
}
first error: inside class GamePlay
check the function DealCards()
check the last lines, two line above the last you have
deckScript.cards.Remove(cur_Card);
you are trying to remove from a list of Deck
a cur_Card
but cur_Card isnt Deck
class is a GameObject
if you are sure to cur_Card have a Deck
script attached do this
deckScript.cards.Remove(cur_Card.GetComponent<Deck>());
otherwise put a if condition
Deck scur_Card = cur_Card.GetComponent<Deck>();
if(scur_Card != null)
deckScript.cards.Remove(scur_Card);
second error: on the same function, above the Debug.Log,you are trying to set a item from a Deck list, to is Deck
type to a GameObject
type variable, just change
cur_Card = deckScript.cards[cardIndex];
by
cur_Card = deckScript.cards[cardIndex].gameObject;
That did remove the errors. However, for some reason it changed how my list shows up in the inspector. I can no longer add game objects or assign values to them per the class Deck.
To be clear, the way it was when the errors were present, I was able to drag and drop GameObjects from the Hierarchy into the inspector adding to the list cards in CardDeck. It appears that once I make this change cur_Card = deckScript.cards[cardIndex].gameObject;
I can no longer drag and drop.
make sure to each of the cards you are trying to drag & drop, have a least a Deck script attached
Answer by Vicarian · Jan 28, 2019 at 06:05 PM
Change the declaration of public GameObject cur_Card;
in GamePlay.cs to public Deck cur_Card;
That brought on new issues of "Deck" not containing definitions for Active and SetActive.
You need to be aware of type. SetActive and similar methods are members of GameObject, so to set them, you need to dereference your list of Deck objects, then access the reference's gameObject property, such as
List<Deck> deckList = new List<Deck>();
// deckList[0] = dereference
// deckList[0].gameObject = access gameObject property of deckList[0] reference
deckList[0].gameObject.SetActive(true);
This did fix the errors. However, it caused the same issue that I was referring to in the comments above from DCordoba.
That did remove the errors. However, for some reason it changed how my list shows up in the inspector. I can no longer add game objects or assign values to them per the class Deck. To be clear, the way it was when the errors were present, I was able to drag and drop GameObjects from the Hierarchy into the inspector adding to the list cards in CardDeck. It appears that once I make these changes I can no longer drag and drop.
Your answer
![](https://koobas.hobune.stream/wayback/20220612190630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to remove an item from a list of custom variables 1 Answer
A node in a childnode? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
List.Add (new CustomClass()) results in empty entry 1 Answer