How to access objects by reference stored in array?
Hello! I dynamically create (during run-time) a set of objects and supposedly store references to them in a two-dimensional array. When I try to get one of stored object through a public method (by call from another script if it matters) I get NULL. Perhaps I'm missing something about scopes or what the heck is going on? Field property contains prefab.
public class Field : MonoBehaviour {
public GameObject field;
private GameObject[,] FieldSet = new GameObject[5,5];
void Start () {
for (int _i = 0; _i < 5; _i++)
{
for (int _j = 0; _j < 5; _j++)
{
FieldSet[_i, _j] = Instantiate(field);
print(FieldSet[_i, _j]); // prints "field clone"
}
}
}
public void ShowField(int a, int b){ //a = 1, b = 2
print(FieldSet[a,b]); // prints "NULL"
}
}
Answer by TBruce · Apr 06, 2016 at 11:52 PM
@Williamsii Your script works fine. I added an Update method to the class and iterated through all game objects in the field set printing out their name (using Debug.Log). Are you sure that "public GameObject field" is initialized?
public GameObject field;
private GameObject[,] FieldSet = new GameObject[5,5];
private bool initialized = false;
void Start ()
{
for (int _i = 0; _i < 5; _i++)
{
for (int _j = 0; _j < 5; _j++)
{
FieldSet[_i, _j] = Instantiate(field);
}
}
}
public void Update()
{
if (!initialized)
{
initialized = true;
for (int _i = 0; _i < 5; _i++)
{
for (int _j = 0; _j < 5; _j++)
{
ShowField(_i, _j);
}
}
}
}
public void ShowField(int a, int b)
{
if (FieldSet[a, b] != null)
{
Debug.Log("Field[" + a + "][" + b + "].name = " + FieldSet[a, b].name);
}
else
{
Debug.Log("Field[" + a + "][" + b + "] = " + null);
}
}
Field[0][0].name = Field(Clone)
Field[0][1].name = Field(Clone)
Field[0][2].name = Field(Clone)
Field[0][3].name = Field(Clone)
Field[0][4].name = Field(Clone)
Field[1][0].name = Field(Clone)
Field[1][1].name = Field(Clone)
Field[1][2].name = Field(Clone)
Field[1][3].name = Field(Clone)
Field[1][4].name = Field(Clone)
Field[2][0].name = Field(Clone)
Field[2][1].name = Field(Clone)
Field[2][2].name = Field(Clone)
Field[2][3].name = Field(Clone)
Field[2][4].name = Field(Clone)
Field[3][0].name = Field(Clone)
Field[3][1].name = Field(Clone)
Field[3][2].name = Field(Clone)
Field[3][3].name = Field(Clone)
Field[3][4].name = Field(Clone)
Field[4][0].name = Field(Clone)
Field[4][1].name = Field(Clone)
Field[4][2].name = Field(Clone)
Field[4][3].name = Field(Clone)
Field[4][4].name = Field(Clone)
Hello $$anonymous$$avina! thanks for reply. Yes, it seems that calling this method from inside the same class is successful. But my intention was to use it as public method and thats where it fails - on public. got any ideas why?
Then do this:
[HideInInspector] public GameObject[,] FieldSet = new GameObject[5,5];
my mistake was to create new instance of field class, therefore i couldn't get array. stupid me)
Your answer
Follow this Question
Related Questions
Array of Arrays 3 Answers
Array cannot read Greek characters 1 Answer
Randomize text position for 2D Quiz C# 0 Answers
how to change speed of animations in an array? 0 Answers
only keep every 10th entry in an array? 2 Answers