- Home /
Array of GameObjects created with a Script
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 () {
}
}
You need to allocate the member of Gird_Array as well as the space for the array presu$$anonymous$$g the thing in the array is a class and not a structure.
Gird_Array[i,j] = new Gird_Node();
Gird_Array[i,j].Node = Instantiate(pPrefab,...
Answer by DaveA · Oct 27, 2012 at 12:46 AM
A million is a lot to expect, good luck.
That said, try using float instead of int for i and j, or cast differently:
new Vector3((float)i,(float)j,0.0f)
and you don't need to initialize like this, just let it be null cuz it will be overwritten anyway and it's usually bad practice to use 'new' in constructors, do 'new' in Start if possible
public GameObject Node; //that's good enough = new GameObject();
Instantiate(pPrefab, new Vector3((float)i*1.0F,(float)j*1.0F,0.0F), Quaternion.identity); works perfectly, but I want this to be a gameobject in a C# class or array and don't care which as I need to assign separate params to each node.
having just
Instantiate(pPrefab, new Vector3((float)i*1.0F,(float)j*1.0F,0.0F), Quaternion.identity);
is of no use to me.
Still nothing, I get
NullReferenceException: Object reference not set to an instance of an object on line Gird_Array[i,j].Node = Instantiate(pPrefab, new Vector3(i*1.0F,j*1.0F,0.0F), Quaternion.identity) as GameObject;
Why does none of the other help questions I look at work. Id be better off writing my own game engine for android in C++, at least C++ works unlike java and C#
You can't handle 1 million game objects in Unity: at most 64$$anonymous$$, but not at practical frame rates. To make something usable in a desktop you should work with 1000 or less game objects - a lot less, if you're developing for Android devices...
That's not an Unity fault: allocating and managing 1 million memory blocks is extremely slow in any system or language.
Answer by SarahWIld · Oct 27, 2012 at 04:13 AM
GameObject Nodes[i,j] = Instantiate(pPrefab, new Vector3((float)i*1.0F,(float)j*1.0F,0.0F), Quaternion.identity) as GameObject;
This works, but with a class, it don't. So I will have to have a Class[,] and GameObject[,] Separate. Why is Unity so crap as the most basic function levels. This surely cane be how shit C# is.
Ok - so why do you want these things to be game objects? If you really have 1m independent game objects then there is going to be a massive overhead - a game object is effectively going to be a draw call (though batching might reduce it). Your game model sounds like it needs to be manipulating your own classes rather than prefabs and perhaps constructing meshes that can be moved.
You are wrestling with two things here - one is the switch from C++ to C# - C# has no problem making a 1m instances of a class.
public class SomeClass
{
public string someString = string.Empty;
public object someObject;
}
SomeClass[,] array = new SomeClass[1000,1000];
for(var i = 0; i < 1000; i++)
{
for(var j = 0; j < 1000; j++)
{
array[i,j] = new SomeClass();
}
}
But you are trying to make part of the Unity framework with a GameObject - a prefab is a heavy weight thing, it probably has a renderer, an animation etc etc - Unity as a framework isn't designed to have 1m of those - it just isn't going to work.
Your best bet would be to create your own system within Unity to handle the game design you are after and benefit from all of the places where Unity will help you. $$anonymous$$oving 1m things using GameObjects isn't the design objective in Unity - but, for example, you could have a whole bunch of bones that moved parts of a mesh.
At the end of the day all systems have a basic design that they are scoped to produce, it is certainly possible that Unity isn't for you on this project.