- Home /
Question by
ogito123 · Oct 15, 2020 at 02:55 PM ·
matrixarray of gameobjects
Matrix losing reference to GameObject?
I have a gameObject private GameObject[,] gridAsMatrix = new GameObject[5,5];
and I have a CreateGrid function that's called on Start. It is as follows.
void CreateGrid(Transform mapHolder)
{
for(int i=0;i<gridHeight;i++)
{
for(int j=0;j<gridWidth;j++)
{
GameObject newHex= Instantiate(hexagonPrefab);
Vector2 gridPos=new Vector2(i,j);
newHex.transform.position = CalcWorldPos(gridPos);
newHex.transform.SetParent(mapHolder,false);
newHex.name=i.ToString() + j.ToString();
Debug.Log(newHex.name);
gridAsMatrix[i,j]=newHex;
Debug.Log(gridAsMatrix[i,j].name);
}
}
}
The two debug statements above work correctly and give the same output. Yet in my Update function I have the following statement
void Update()
{
PropagateDown();
}
void PropagateDown()
{
for(int i=0;i<hexHeight;i++)
{
for(int j=0;j<hexWidth;j++)
{
Debug.Log(gridAsMatrix[i,j].name);
}
}
}
but this time the gridAsMatrix[i,j].name gives out "00" as its output always. Why did my values change? I am also using an editor script but I don't think its the cause of the issue. I can give that also if this isn't enough.
Comment
Do gridHeight and hexHeight match? As well as gridWidth and hexWidth?
I cant believe that was the mistake. I spend 7 hours on this. Sometimes being a software developer is miserable.... Thank you very much.
Your answer
![](https://koobas.hobune.stream/wayback/20220613013840im_/https://answers.unity.com/themes/thub/images/avi.jpg)