I have searched already and found out that I need a twodimesnional array, and its easier thatn it probably sounds but I quite still don’t get it.
So how does one put GameObjects in a twodimensional array or arraylist?
Are you talking about storing an instantiated prefab to access later? You don’t need a 2D array for that. Just do something like this-
GameObject obj = Instantiate(prefab , position, quaternion.identity);
//you've just instantiated a prefab, and 'obj' now holds your instantiated prefab.
For a situation like this, a generic collection like List <>
is probably your best bet.
If for example you want to add all spawned enemies to a single list to manage them you could do this:
public Enemy enemyPrefab;
private List <Enemy> spawnedEnemies = new List<Enemy>();
private void SpawnEnemy (Transform location)
{
Enemy newEnemy = (Enemy)Instantiate (enemyPrefab, location.position, location.rotation);
spawnedEnemies.Add (newEnemy);
}
The Instantiate function returns a reference to the spawned object. You can put that into an array:
var width = 10;
var height = 10;
var arr = new GameObject[width, height]; // for example
for(var x = 0; x < width; x++)
{
for(var y = 0; y < height; y++)
{
arr[x, y] = Instantiate(prefab, new Vector3(x, 0, y), Quaternion.identity);
}
}