Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ChillyRolande · Dec 18, 2021 at 10:00 PM · listinstantiate-game-object

Instantiating GameObjects from a List

I am trying to draw cards from a List 2 at a time in order. Then moving those cards to another list so they are not drawn again on the next click. I almost have it working but the order seems to be off. It is always off in the same order. The lists are populating correctly but in the wrong order [always consistent but still]. *[I do have an error when I draw twice that exception is out of range - I understand I need to do something about this as well, but that is my next task].

I am confused to why the order seems to be off... * Any help is appreciated, I have been doing tons of reading on lists and its getting better bit by bit.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DrawCardsNoRepeat : MonoBehaviour
 {
     public GameObject Card1;
     public GameObject Card2;
     public GameObject Card3;
     public GameObject Card4;
     public GameObject Card5;
     public GameObject Card6;
     public GameObject PlayerArea;
 
     public List<GameObject> Deck = new List<GameObject>();
     public List<GameObject> PlayerHand = new List<GameObject>();
 
     void Start()
 
     {
         Deck.Add(Card1); 
         Deck.Add(Card2);
         Deck.Add(Card3);
         Deck.Add(Card4);
         Deck.Add(Card5);
         Deck.Add(Card6);    
     }
 
     
     public void OnClick()
     {
         for (var i = 0; i < 2; i++) 
         {
             GameObject playerCard = Instantiate(Deck[i], new Vector3(0, 0, 0), Quaternion.identity);
             playerCard.transform.SetParent(PlayerArea.transform, false);
             Deck.Remove(Deck[i]); //tried putting playerCard in here but it doesn't seem to work...
             PlayerHand.Add(playerCard); //it works here though...                
         }   
     }      
 }
 
 




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
Best Answer

Answer by Hellium · Dec 18, 2021 at 10:09 PM

With your current code, you are instantiating and removing the first remaining card of the list, then, the third one of the list. Because you're removing the first one (index 0), Deck[1] is the third element of the "original" list (since all the cards have been shifted to the left when you removed Deck[0].

      for (var i = 0; i < 2 && Deck.Length > 0; i++) 
      {
          GameObject playerCard = Instantiate(Deck[0], new Vector3(0, 0, 0), Quaternion.identity);
          playerCard.transform.SetParent(PlayerArea.transform, false);
          Deck.RemoveAt(0);
          PlayerHand.Add(playerCard);
      }

Note: instead of a List<GameObject>, Deck could be a Queue<GameObject>.

Comment
Add comment · Show 1 · 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 ChillyRolande · Dec 18, 2021 at 10:21 PM 0
Share

WOW @Hellium thank you so much for such a quick response! It worked, except instead of Deck.Length I used Deck.Count. I really appreciate it, I have lots more studying to do. I will ponder over your response so I can fully comprehend it, I think I need more studying on Lists functions, and that for (var i = 0; i < 2 .....) stuff

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

176 People are following this question.

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

Related Questions

How to get tooltips to pop up on certain levels, but not all of them 2 Answers

List Null Ref 1 Answer

Problem with List.Addrange 1 Answer

Trying to do a list of attacks for a Beat em Up. 1 Answer

Having trouble with instantiating bullets at multiple points via a list of transforms. 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