- Home /
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.
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?
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.
$$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.
That makes a lot of sense now!. Thank you again. :)
Your answer
Follow this Question
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