I am trying to create a 2D array of game objects with a script, so upon initialization I get an array of a million or so objects. I C++ this is piss easy, however in Unity it just does not work. No matter what I look at nothing works. I have something I am working but I have no idea on how to create an array of objects. The entire game I’m making will all be in Script form. I don’t want to hard code a million or more nodes.
I have only done a 100*100 array for now.
From this code I get 1 node in centre of screen.
using UnityEngine;
using System.Collections;
public class Gird_Grid : MonoBehaviour {
[System.Serializable]
public class Gird_Node{
public GameObject Node = new GameObject();
};
public GameObject pPrefab;
public Gird_Node[,] Gird_Array = new Gird_Node[100,100];
// Use this for initialization
void Start () {
int count = 0;
for( int i = 0; i < 100; i++)
{
for( int j = 0; j < 100; j++)
{
Gird_Array[i,j].Node = Instantiate(pPrefab, new Vector3(i*1.0F,j*1.0F,0.0F), Quaternion.identity) as GameObject;
count++;
//Gird_Array[i,j].Node.transform.position = new Vector3(i, j, -1);
}
}
}
// Update is called once per frame
void Update () {
}
}