Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how to arrangement child objects using c#

Discussion in 'Scripting' started by elsharkawey, Jun 25, 2018.

  1. elsharkawey

    elsharkawey

    Joined:
    May 15, 2016
    Posts:
    41
    i have a gameobject that have a group of plane meshes that created randomly as child every one of this planes have a "2f" offset in x axis between every plane

    what i wanna do is i wanna make the parent object arrangement the child objects on rows and columns even if one of this child deleted during the game or added so if i have 8 planes mesh it should look like that 2 rows and 4 columns like that but i don't know how to do that ?





    i know how to get child objects and making offset but i do't know how to get the format like that in the image i wanna to arrangement the planes meshes that i can acces from this for loop

    Code (CSharp):
    1.  void Update(){
    2.           Transform[] children = new Transform[transform.childCount];
    3.                  //Debug.Log(transform.childCount);
    4.                      for (int i=0; i<transform.childCount; i++) {
    5.                          children[i]=transform.GetChild(i);
    6.                            
    7.                      }
    8.       }
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    First of all, you are generating a new Transform array every frame! Thats not a good idea to do. So you want to offset x and y axis, right? So, if your x-axis gos through the max offset of 2*4 = 8, it should offset to y, right? You also want to use List<> instead of that array and add them on start, so you can delete them on runtime and check for the count etc.
     
  3. elsharkawey

    elsharkawey

    Joined:
    May 15, 2016
    Posts:
    41

    thanks for your reply yes right so i should make something like that transform.childCount * 2 /4 to get the columns numbers then the columns numbers * 2 to get the y axis offset value that what you mean ?

    are my list part better now ?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Coulmnandrowsformating : MonoBehaviour {
    6.  
    7.  
    8.     private GameObject[] children;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.      
    13.              
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.                      //Transform[] children = new Transform[transform.childCount];
    21.                     Debug.Log(transform.childCount);
    22.                     children = new GameObject[transform.childCount];
    23.                     for (int i=0; i<transform.childCount; i++) {
    24.                         children[i]=transform.GetChild(i).gameObject;
    25.  
    26.                     }
    27.  
    28.  
    29.  
    30.     }
    31. }
     
    Last edited: Jun 25, 2018
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    You dont need it in update. Just put in in a coroutine and run it whenever the pile number changes. Thats what i was talking about. then you can check if the i is > 4, if it is, offset the y value of your transforms and put the x value back to the first value.