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 IGOR824 · Mar 05, 2020 at 03:02 PM · gameobjectlistchild

Removing index of an integer list from a child Game Object

Hey there,

I'm having a problem with transform child, when this program runs it currently doesn't correctly delete the elements in the deck list and adds the incorrect elements into the discard pile list. I'm certain that child.childCount is not correct for finding the correct child at its index in the list which is why the deck and discard pile lists are not working as intended.

Basically what is supposed to happen is that the elements from the list of deck are supposed to go into discard pile and then reset when there are no more elements left in deck.

 public void DealFromDeck()
         {
             //add remaining cards to discard pile
             foreach (Transform child in deckButton.transform)
             {
                 if (child.CompareTag("Card"))
                 {
                 deck.Remove(child.childCount);
                 discardPile.Add(child.childCount);
                 Destroy(child.gameObject);
                 }
             }
             if (deckLocation < draw)
             {
                 //draw card
                 drawOnDisplay.Clear();
                 float xOffset = 2.0f;
                 foreach (int card in deckDraw[deckLocation])
                 {
                     GameObject newTopCard = Instantiate(cardFaces[card], new Vector3(deckButton.transform.position.x + xOffset, deckButton.transform.position.y, deckButton.transform.position.z), Quaternion.Euler(0, 0, Random.Range(0.0F, 1.0F) < 0.5F ? 0.0F : -180.0F), deckButton.transform);
                     GameObject artAnimation = Instantiate(cardContainer, new Vector3(deckButton.transform.position.x + xOffset, deckButton.transform.position.y, deckButton.transform.position.z), Quaternion.Euler(0, 0, Random.Range(0.0F, 1.0F) < 0.5F ? 0.0F : -180.0F), deckButton.transform);
                     artAnimation.transform.parent = newTopCard.transform;
                     drawOnDisplay.Add(card);
                     xOffset = xOffset + 1.5f;
                 }
                 deckLocation++;
             }
             else
             {
                 //restack the top deck
                 RestackTopDeck();
             }
         }
     
         void RestackTopDeck()
         {
             foreach (int card in discardPile)
             {
                 deck.Add(card);
             }
             discardPile.Clear();
             SortDeck();
         }

Comment
Add comment · Show 1
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 austephner · Mar 05, 2020 at 04:42 PM 0
Share

child.childCount is the number of children that transform has, not the index of where its at amongst its siblings

https://docs.unity3d.com/ScriptReference/Transform-childCount.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by RedBambooLeaf · Mar 05, 2020 at 04:40 PM

Transform.childCount returns

The number of children the parent Transform has.

Excluding itself.


I presume your field deck is of type List<T> where T is the type of your card. If that's the case, then please note that Add and Remove take a T element as an argument. So, unless your card are of type int, you're add/remove don't make much sense.


Iterating through a Transform is equivalent to iterating through the immediate children of that transform so, if your transform has a number of levels of hierarchy beneath it, you won't take into account any nested children. If you want to make sure you have all the children, use GetComponentsInChildren();


So, what's wrong here?

  1. Are you sure you're visiting all the children? foreach (Transform child in deckButton.transform)

  2. Are you sure you want to add/remove from your deck a number? deck.Remove(child.childCount);


I didn't check the rest of the code... But I guess this should be more than enough to start fixing it.

Hope it helps!

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
avatar image
0

Answer by tecses1 · Mar 05, 2020 at 04:57 PM

Yeah. child.childCount is not an index, but rather a quantity of children this object has. If you are determined in finding children with the foreach iteration through the transform, then use childCount as the index count. So:

 for (int I = 0; I < transform.childCount; I++){
     if (transform.GetChild(I).tag == "Card"){
          deck.Remove(child.childCount);
          discardPile.Add(child.childCount);
          Destroy(child.gameObject);
     }
 }

I would strongly recommend however, creating your own Deck Handler class, that has game object lists for each card list you want, and specific function handlers for each. So:

 public class DeckHandler : MonoBehavior{
     public List<Transform> drawPile = new List<Transform>();
     public List<Transform> hand = new List<Transform>();
     public List<Transform> discardPile = new List<Transform>();

     //Functions to call for handling.
    void Reset(){
        foreach (Transform card in hand){
            discardPile.Add(card);
            hand.Remove(card);
        }
    }
   void Draw(){
      hand.Add(drawPile[drawPile.Count-1]);
      drawPile.Remove(drawPile[drawPile.Count-1]);
  }
  void Play(int index){
      hand[index].play();
      hand.RemoveAt(index);
  }
  //etc etc
 }

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

197 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 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

A node in a childnode? 1 Answer

"Center On Children" programmatically 1 Answer

Can't Activate a GameObject from Array 1 Answer

How to Access Components in Children of GameObject? 1 Answer

Instantiate as child 3 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