- Home /
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();
}
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
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?
Are you sure you're visiting all the children?
foreach (Transform child in deckButton.transform)
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!
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
}
Your answer
Follow this Question
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