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 /
  • Help Room /
avatar image
0
Question by Linarib · Apr 18, 2016 at 02:30 PM · arraylistlistscardcards

Creating card game - copy of list to new list - creating cycle

Hi, I am new in Unity and I am trying to make a card game (not classic cards).

I have a deck from 40 cards. 10 cards are added to one panel, 3 cards to another one and then 27 left are displayed in another panel on button press (player see only one, but it's opening 3 cards). Everything works perfect, but when those 27 cards ends I need them to go back to the same list that player could press button and see it again that it would work as a cycle.

I was trying to create one more list and add those there, but the result is that after the last card on button press it shows only that one (since the function has to open three cards at one press so it opens the same card three times). This is my code. Does someone have an idea how can I fix it?

 public GameObject number; //for number of deck array
     
 public List<GameObject> deck = new List<GameObject>();
 private List<GameObject> displayed = new List<GameObject>();
 
 //it's code only for those 27 cards that left
 public void OpenThreeCards(GameObject panel)
     {
         for (int i = 0; i < 3; i++) {
             
             int number = deck.Count - 1;
 
             if (number == 0) {
                 deck = displayed;
             }
 
             GameObject k;
             int r;
             r = 0;
             k = Instantiate (deck [number]);
             k.transform.SetParent (panel.transform);
         
             k.GetComponent<RectTransform> ().localRotation = Quaternion.identity;
             displayed.Add (deck [number]);
             deck.Remove (deck [number]);
         }
 
     }
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
0

Answer by troien · Apr 18, 2016 at 03:40 PM

I'm not sure whether i understand what you want, so I'm going to interpret your explanation as best as I can :p

You have 2 lists, deck and displayed.

  1. At start, deck has 27 cards, displayed has 0 cards.

  2. At the end displayed has 27 cards, deck has 0 cards.

When 2 hapens you want to go back to 1.

You currently call deck = displayed, I suppose you do that in an effort to achieve this. However, the first thing thats wrong with this is that List is a reference type, not a value type. When you set deck to displayed. They are from then on the same object (not just 2 objects with the same value). Which means that changes to deck are also applied to displayed and vise versa. you probably want to do deck = new List(displayed) instead. As this would create a new list with the same values. Secondly, you do this before you do something with the last card. I'm assuming this is not intentional, not sure though :p

I think you want to do something like this:

 public void OpenThreeCards(GameObject panel)
 {
     // assumes amount of cards (27) can be devided by 3 (3, 6, 9, 12, etc.)
     for (int i = 0; i < 3; i++)
     {
         int number = 0; // first instead of last to keep displayed in the same order as deck
 
         GameObject k;
         k = Instantiate(deck[number]);
         k.transform.SetParent(panel.transform);
 
         k.GetComponent<RectTransform>().localRotation = Quaternion.identity;
         displayed.Add(deck[number]);
         deck.Remove(deck[number]);
     }
     if (deck.Count == 0)
     {
         deck = new List<GameObject>(displayed);
         displayed.Clear();
     }
 }

Another thing you might want to consider for efficiency is using 2 Queues instead Lists.

Comment
Add comment · Show 4 · 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
avatar image Linarib · Apr 18, 2016 at 06:34 PM 0
Share

Thank you! Yes, it works perfectly!

avatar image Linarib · Apr 19, 2016 at 11:08 AM 0
Share

Ok, it works but..
if (deck.Count == 0) { deck = new List<GameObject>(displayed); displayed.Clear(); } It gives plus 27 cards to deck, but doesn't remove all those 27 cards that were displayed before. So after this I have 27 cards + 27 cards + more and more... But I need that in the deck would be only 27 cards

avatar image troien Linarib · Apr 19, 2016 at 02:55 PM 0
Share

With removing, do you mean removing from the list? Because they are removed from the list. The list should never be greater then 27. Or do you mean removing the instantiated gameobjects themselves?

If you mean removing the instantiated gameobjects. I don't know when you intend to destroy them, only on reset or while playing aswell?, as currently you are not holding a reference to your instantiated gameobject anywhere. ("GameObject k" is your instantiated gameobject, but you don't store this reference anywhere in your code) So if you want to destroy all 27 insantiated gameobjects at the moment you reset. You'll need to store the reference in a list or something, and call Destroy() for each item in that list when you reset. Then also clear hat list at reset.

And are you using the displayed list anywhere outside of the sniped of code you posted?

avatar image Linarib troien · Apr 20, 2016 at 04:19 PM 0
Share

Yes, Distroy() works perfect. Thank you!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Find the closest Enemy that has a tag added in a list? 1 Answer

Getting the element that has the same element number on another list ? 2 Answers

I have trouble understanding arrays and enums. When and how? 0 Answers

Compare GameObject with an array 1 Answer

Array (List) with multiple variable types? 2 Answers


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