how to access list from another class (list of cards on deck class) ?

i’m new on unity, i want to make a Card Game. in my making use of class card to represent the attributes of that class. then I used a list of cards in the deck class to hold classes there. i want to manipulate deck card, remove it, move its content ect.

  1. how do I access the list on class deck from the other class?.
  2. how I can move the contents from list of classes into a new list in another class?
  3. if you all have another better way, please tell me :slight_smile:
  4. Please evaluate me if i’m wrong when implementing the code.

This my cards class

public class Cards : MonoBehaviour {

    
   private string _type;
private string _action;
    private int _gold;

    

    public string Type
    {
        get { return _type; }
        set { _type = value; }
    }
    public string Action
    {
        get { return _action; }
        set { _action = value; }
    }

    public int Gold
    {
        get { return _gold; }
        set { _gold = value; }
    }

my Deck Class

public class DeckCard : MonoBehaviour {
   
    public List<Cards> _cardsOnDeck;
    HandCard hand;
    void Start()
    {
        FirstDeck();
        
    }

    void FirstDeck()
    {
        _cardsOnDeck = new List<Cards>();

        for(int i=0; i < 5; i++)
        {
            Cards cardGold = new Cards();
            cardGold.Type = "Resource";
            _cardsOnDeck.Add(cardGold);
        }
        for (int i = 0; i < 2; i++)
        {
            Cards cardAction = new Cards();
            cardAction.Type = "Resource";
            _cardsOnDeck.Add(cardAction);
        }

        
    }

my hand card class

public class HandCard : MonoBehaviour {
    [SerializeField]
    private List<Cards> _cardOnHand = new List<Cards>();
    private DeckCard _drawedCard = new DeckCard();
    public GameObject SpawnCard;



    void Start()
    {

        _cardOnHand.AddRange(_drawedCard._cardsOnDeck);
    }

    void update()
    {
        Debug.Log(_cardOnHand[0].GetType());
    }


    Cards DrawCard()
    {
        if (_drawedCard._cardsOnDeck.Count == 0)
        {
            return null;
        }
        else
        {
            int _cardIndex = Random.Range(0, _drawedCard._cardsOnDeck.Count - 1);
            Cards cardGO = _drawedCard._cardsOnDeck[_cardIndex];
            _drawedCard._cardsOnDeck.RemoveAt(_cardIndex);

            return cardGO;

        }

    }


    public void DrawCardOnHand()
    {
        
        Cards spawnNew = Instantiate(SpawnCard, transform.position, Quaternion.identity).GetComponent<Cards>();

        for (int i = 0; i < 5; i++)
        {
            _cardOnHand.Add(DrawCard());
            spawnNew = _cardOnHand*;*

}
}

void removeCard()
{
int x = _drawedCard._cardsOnDeck.Count;
Debug.Log(x);
}
thanks for attention, sorry for my bad english :slight_smile:

  1. you can get the reference to list of cards by doing myDeckClassInstance._cardsOnDeck; which in effect represents a list.

But a safe way would be to clone the list first, before giving it to whoever requests it. Right now, if we decided to add a new instance of cards, myDeckClassInstance._cardsOnDeck.Add(new Cards()};this will insert a card into the original list.

if inside of DeckCard class you specify

public List<Cards> cardsOnDeck{ 
      //create a new list, based-off the _cardsOnDeck
      get{return new List<Cards>(_cardsOnDeck); }
};

and make original, _cardsOnDeck as private. This way you will prevent anyone from outside being able to modify your list.

Anyone who will be calling cardsOnDeck will be getting clone of the list. They can go nuts with it or even set it to null, but it will not affect the original list and it will still be accessible via _cardsOnDeck, concealed variable

  1. you can either point myNewListFromPrevOne = myDeckClassInstance._cardsOnDeck;

or clone it, like explained above.

.

by the way, you’ve got a very very good coding style.