Search Unity

Question Adding child objects of parent to List

Discussion in 'Scripting' started by marin7676, Jun 1, 2020.

  1. marin7676

    marin7676

    Joined:
    Apr 9, 2019
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5.  
    6. public class BoardManager : MonoBehaviour
    7. {
    8.     private List<Transform> pathFieldsPositions = new List<Transform>();
    9.     private List<Transform> finishFieldsPositions = new List<Transform>();
    10.     private List<Transform> homeFieldsPositions = new List<Transform>();
    11.     private Transform pathFields;
    12.     private Transform finishFields;
    13.     private Transform homeFields;
    14.     public GameObject player;
    15.  
    16.  
    17.     private void Awake()
    18.     {
    19.         this.gameObject.SetActive(true);
    20.  
    21.         pathFields = this.transform.Find("PathFields");
    22.         finishFields = this.transform.Find("FinishFields");
    23.         homeFields = this.transform.Find("HomeFields");
    24.  
    25.         int childCountPathFields = pathFields.childCount;
    26.         int childCountFinishFields = finishFields.childCount;
    27.         int childCountHomeFields = homeFields.childCount;
    28.  
    29.         //Adding the children of the Path Fields game object to list
    30.         for (int i = 0; i < childCountPathFields; i++)
    31.         {
    32.             pathFieldsPositions.Add(pathFields.GetChild(childCountPathFields - 1));
    33.         }
    34.  
    35.         //Adding the children of the Finish Fields game object to list
    36.         for (int i = 0; i < childCountFinishFields; i++)
    37.         {
    38.             finishFieldsPositions.Add(finishFields.GetChild(childCountFinishFields - 1));
    39.         }
    40.  
    41.         //Adding the children of the Home Fields game object to list
    42.         for (int i = 0; i < childCountHomeFields; i++)
    43.         {
    44.             homeFieldsPositions.Add(homeFields.GetChild(childCountHomeFields - 1));
    45.         }
    46.  
    47.         Debug.Log("Finish fields positions count is:" + finishFieldsPositions.Count);
    48.         Debug.Log("Path fields positions count is:" + pathFieldsPositions.Count);
    49.         Debug.Log("Home fields positions count is:" + homeFieldsPositions.Count);
    50.  
    51.         for (int i = 0; i < homeFieldsPositions.Count; i++)
    52.         {
    53.             Debug.Log(homeFieldsPositions[i].transform.position);
    54.         }
    55.     }
    56. }
    57.  

    I saved all child positions of 3 types of fields into Lists of Transform, but for some reaosn, when I want instantiate some game object on positions of items of these 3 lists, all game Objects are always instantiated on a one single component of lists, so all of them are at the same position of one Transform component from the lists.
    What am I doing wrong here?

    Thanks a lot for the help
     
    Last edited: Jun 1, 2020
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Use code tags, it'll make your code easier to read, also will prevent your code from mangling your post (like, your [ i ] made the remainder of your post italics, for example)
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Use code tags. That's your first mistake.

    Second, I'm confused as to what you are trying to do. Your for loops suggest you want to start from child 0 and go to the max child.

    But then when you call GetChild, instead of using "i" you use childCountHomeFields -1, which means you'll always get the last child. You are always going to get the same child over and over again.
     
  4. marin7676

    marin7676

    Joined:
    Apr 9, 2019
    Posts:
    2
    Hello,sorry for mistakes, its my first times posting here, hope you dont blame me :)

    Problem solved, I should have just put the variable i into code, and not those ChildCounts.

    Thanks a lot for your help and time!