- Home /
2D Array of GameObjects...
Hey Guys, This should be really simple but I have no idea why its not working... I initialize a 2d array then try to create a gameobject and store it at each index, but it keeps giving the error "Object not set to an instance of an object" I have the gameobject set in the inspector, and same with the row height and column length... Please help,
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MeteorSpawn : MonoBehaviour {
public GameObject TestMeteor;
public int ColumnLength;
public int RowHeight;
public GameObject [][] meteorArray;
// Use this for initialization
void Start () {
for(int i = 0; i<ColumnLength; i++){
for(int j=0;j<RowHeight; j++){
meteorArray[i][j] = (GameObject)Instantiate(TestMeteor,new Vector3(i,j,0),Quaternion.identity) ;
}
}
}
// Update is called once per frame
void Update () {
}
}
Answer by hbalint1 · May 09, 2015 at 07:40 AM
What you are trying to use is an array of arrays, not a two dimensional array. Here you can read about the difference: http://stackoverflow.com/questions/12567329/multidimensional-array-vs
Here's the fixed class:
using UnityEngine;
using System.Collections;
public class MeteorSpawn : MonoBehaviour {
public GameObject TestMeteor;
public int ColumnLength;
public int RowHeight;
public GameObject[,] meteorArray;
// Use this for initialization
void Start()
{
meteorArray = new GameObject[ColumnLength,RowHeight];
for (int i = 0; i < ColumnLength; i++)
{
for (int j = 0; j < RowHeight; j++)
{
meteorArray[i,j] = (GameObject)Instantiate(TestMeteor, new Vector3(i, j, 0), Quaternion.identity);
}
}
}
// Update is called once per frame
void Update()
{
}
}
I already try to do that, But I can't drag and drop like public value usually. if I using other methods, I'm not sure I can control the placement of each element is there another solution that I can drag and drop to fill the array?