Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tioazis · Mar 12, 2017 at 10:29 PM · c#listoopcards

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 :)

  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[i];
 
         }
     }
 
 
     void removeCard()
     {
        int x = _drawedCard._cardsOnDeck.Count;
         Debug.Log(x);
     }






thanks for attention, sorry for my bad english :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by IgorAherne · Mar 13, 2017 at 01:53 AM

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

2) 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

Accessing Smartfox room list from another script in C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges