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 /
avatar image
0
Question by RealNiv · Sep 28, 2020 at 04:11 PM · transformcards

children are not in the parent transform?

So I am trying to empty a card pile and I iterate over the children of the pile game object but only some of them are affected (maybe some are only children in the hierarchy?). This is the code:

 public void TransferCardsTo(PlayerManager player)
     {
         FlipCards(pile);
         player.deck.AddRange(pile);
         pile.RemoveRange(0, pile.Count);
         
         foreach (Transform child in this.transform)  // moving the cards from the pile to the player's deck
         {
             Debug.Log(child.transform);
             child.transform.SetParent(player.transform,false);
 
         }
 }


And I have only one draw function so I couldn't figure out what can diffrentiate between these objects.

 public CardClass PullCard()
     {
         GameObject card = deck[0];
         card.transform.SetParent(cardPile.transform,false);
         card.transform.position = cardPile.transform.position;
         card.GetComponent<CardFlipper>().Flip();
         cardPile.GetComponent<Pile>().AddCard(deck[0]);
         deck.Remove(deck[0]);
         return card.GetComponent<CardClass>();
     }


Anyone knows what is the problem?

In the hierarchy, some of the cards game objects stay as children of the pile game object and some does not.

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 unity_ek98vnTRplGj8Q · Sep 28, 2020 at 06:24 PM

Interesting... just for curiosity's sake can you try

 List<Transform> children = GetComponenstInChildren<Transform>();
 foreach (Transform c in children){

Instead of your current foreach loop?

Comment
Add comment · Show 3 · 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 RealNiv · Sep 28, 2020 at 07:20 PM 0
Share

GetComponentsInChildren returns an array so I tried it with an array instead of a list, and it works but it also moves the pile game object . Edit: Thank you. I fixed it by just using a normal for loop from i=1 to children.Length. Still dont understand what has caused my older code to work though.

avatar image unity_ek98vnTRplGj8Q RealNiv · Sep 28, 2020 at 08:21 PM 1
Share

$$anonymous$$y guess is that it has to do with how the transform iterates through its children. For example, if the iterator looked something like this (sudocode)

 int i = -1;
 
 get next {
    i = i + 1;
    return transform.GetChildAtIndex(i);
 }

Then what would happen? Take index = 1 for example. The iterator will return the child object (let's call it "A") at index 1 (this is the first object, assu$$anonymous$$g the parent itself is index 0). All well and good. But then you remove A from the list of children. Now the second child object (B) moves to index 1 of the children, since there is no object in that spot. Object C then moves to occupy index 2 in the list of children and so on. Next time your iterator asks for the next child object, it will look at index 2. But this is where C is since B has moved to index 1. So B has essentially been skipped by the iterator, as will every second object in our list of children. This is because we are iterating through a collection while the collection is changing. If you have some experience with program$$anonymous$$g, you will know that this is a big no-no, and will usually throw an error. It doesn't throw an error in this case (I'm assu$$anonymous$$g) because this is not an actual collection that is being iterated through, rather transform is just looking at its child objects (but we still run into the same issue that we would with a normal collection). The fix to this is to generate our list of objects beforehand, then when we remove them from the children of the parent transform we aren't removing them from our collection.

avatar image RealNiv unity_ek98vnTRplGj8Q · Sep 29, 2020 at 04:38 AM 0
Share

That makes a lot of sense now!. Thank you again. :)

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

175 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

Related Questions

Move cards around mouse as pivot 1 Answer

Move to a location while rotating. 0 Answers

FBX Import model won't change transform in playmode, only in Scenepreview. Why? 1 Answer

Detect if an object in 3D space is within/touches a UI image 0 Answers

How to prevent the cloth sim from breaking after changing the transform.parent of an ancestor 0 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