Make a grid and turn it to List
Hi, i just start programming for unity 3d (3-4 days from now %) ) and make a simple script to make grid from cubes (you can choose how many cells you want to make) and its working... but i think its to complicated what i can change for simplicity? And one more question... how to sort this list? Thanks guyz!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CreateAGridFromCube : MonoBehaviour {
public GameObject cube;
public int horCells;
public int verCells;
public List<GameObject> cellsList = new List<GameObject>();
// Use this for initialization
void Start () {
cellsList.Add (cube);
MakeGrid (horCells,verCells);
}
void MakeGrid(int hor, int vert){
for (int x = 0; (x + 1) < hor; x++) {
GameObject clone = Instantiate (cube);
clone.transform.position = new Vector3(clone.transform.position.x+1,clone.transform.position.y,clone.transform.position.z);
clone.name = "1x" + (x + 2);
for (int y = 0; (y+1) < vert; y++) {
GameObject cloneV = Instantiate (cube);
cloneV.transform.position = new Vector3(cloneV.transform.position.x,cloneV.transform.position.y-1,cloneV.transform.position.z);
cloneV.name = (y + 2)+ "x" + (x + 1);
cellsList.Add (cloneV);
cube = cloneV;
}
cellsList.Add (clone);
cube = clone;
}
int z = cellsList.Count - 1;
GameObject fuckingLastClones = cellsList [z];
for (int i = 0; (i+1) < vert; i++) {
GameObject LastLastClones = Instantiate (fuckingLastClones);
LastLastClones.transform.position = new Vector3(LastLastClones.transform.position.x,LastLastClones.transform.position.y-1,LastLastClones.transform.position.z);
LastLastClones.name = (i+2) + "x" + hor;
cellsList.Add (LastLastClones);
fuckingLastClones = LastLastClones;
}
}
}
Answer by OncaLupe · Dec 19, 2015 at 04:29 AM
You definitely made the spawning much more complicated than is needed. For a static length grid I'd also use a 2d array, so that if you need to access a specific grid cube you can just pass in the x and y.
using UnityEngine;
using System.Collections;
public class CreateAGridArrayFromCube : MonoBehaviour
{
public GameObject cube;
public int horCells = 10;
public int verCells = 10;
public Vector3 startPos = new Vector3(0f, 0f, 0f);
public float spacingX = 1f;
public float spacingY = 1f;
public GameObject[,] cellsArray;
void Start()
{
MakeGrid(horCells, verCells);
}
void MakeGrid(int hor, int vert)
{
cellsArray = new GameObject[hor,vert];
GameObject clone;
Vector3 clonePos;
for(int x = 0; x < hor; ++x)
{
for(int y = 0; y < vert; ++y)
{
clonePos = new Vector3(startPos.x + (x * spacingX), startPos.y + (y * spacingY), startPos.z);
clone = Instantiate(cube, clonePos, Quaternion.identity) as GameObject;
clone.name = (y + 1) + "x" + (x + 1);
cellsArray[x,y] = clone;
}
}
}
}
If you'd rather use the List, then you can replace these lines:
public GameObject[,] cellsArray;
cellsArray = new GameObject[hor,vert];
cellsArray[x,y] = clone;
with these:
public List<GameObject> cellsList;
cellsList = new List<GameObject>();
cellsList.Add(clone);
Then, to access a specific grid cube from the List (remember to start counting from 0):
cellsList[(x * verCells) + y];
Tnx for your help! Try to make this code better :) I like idea with 2D array, try to use it practically!
Your answer
Follow this Question
Related Questions
Why is my List adding to same array object again and again in update? 1 Answer
Dynamically Populated Dropdown list has items with the same text 1 Answer
List returns NullReferenceException, but when it's visible in the Inspector it works perfectly ??? 1 Answer
Problem adding List<> objects to another List<> 1 Answer
How to remove items from a target list on a target object's defeat. 1 Answer