Search Unity

Rearranging pre-existing objcets into a grid

Discussion in 'Scripting' started by graviton, Apr 6, 2015.

  1. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    This really should be simple, but I can't seem to rap my head around the logic

    I'm trying to create a grid using objects that already exist in the world. I can create a grid as the objects are instantiated just fine, but can't seem to do it with pre-existing objects

    In my Hierarchy, I have an EmptyGameObject named "myEmptyGameObject" and it has 4 Child Objects

    Here's my code
    Code (CSharp):
    1.  
    2. //Re-arranges pre-existing objects into a Row, Column or Grid
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class listToGridTest : MonoBehaviour
    8. {
    9.    
    10.     public float newY;
    11.     public float newX;
    12.     bool finishedSpawning;
    13.    
    14.     void Start()
    15.     {
    16.         StartCoroutine(SortCoroutine());
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         if (!finishedSpawning)
    23.             return;
    24.     }
    25.  
    26.  
    27.     private IEnumerator SortCoroutine()
    28.     {
    29.         finishedSpawning = false;
    30.        
    31.         Transform otherGameObject = GameObject.Find("myEmptyGameObject").GetComponentInChildren<Transform>();
    32.        
    33.         int grids = otherGameObject.childCount;
    34.         for (int i = 0 ; i <= grids - 1; i++)
    35.         {
    36.             for (int y = 2; y > 0; y--)
    37.             {
    38.                 newY += y*2;
    39.                 otherGameObject.GetChild(i).transform.position = new Vector3(newX, newY, 1);
    40.                
    41.                 for (int x = 0; x < 2; x++)
    42.                 {
    43.                     newX += x*2;
    44.                     otherGameObject.GetChild(i).transform.position = new Vector3(newX, newY, 1);
    45.                 }
    46.                 yield return new WaitForSeconds(1f);
    47.             }
    48.         }
    49.        
    50.         finishedSpawning = true;
    51.     }
    52.  
    53. }
    54.  
    I seem to have hit a mental block
     
  2. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    I'm not entirely sure about what's going on because of your rather undescriptive naming scheme ("other game object" and "my empty game object"), but I believe you are trying to sort the four children of myEmptyGameObject, yes?

    You get the Transform of one of the children of the myEmptyGameObject GameObject, which is the parent object holding the four children you intent to re-arrange. Then, you try to get a child from that child, instead of getting the children of myEmptyGameObject. What you should be doing instead is using the transform of myEmptyGameObject directly, and then moving the transforms of its children. (GameObject.Find("myEmptyGameObject").transform)

    Also, you're positioning the same object many times over. In the outer most loop, you pick the child object you want. Then, you move that object twice in the y loop, and in that y loop you move it twice in the x loop again; that's a lot of needless moving about. I see there's a WaitForSeconds so maybe you're animating the movement into the grid, but only after doing the x loop, so the movement in that loop isn't being displayed.
     
    graviton likes this.