Hello, I am fairly new to Unity and have limited experience with C#, I have been struggling with this problem for a couple of days and was not able to find a solution on forum.
I have a list “Matrix” that is being created in runtime. When GetList() is called from Start(), the Debug.Log shows correctly the size of the list.
But if I call GetList() from another class or I try to access the list in anyway in play mode, the size count of the list is always 0 and I cannot access the list items (of course).
I need to access this list during play mode to retrieve and update values dynamically… but I can’t since the it seems to reset to size 0 during play mode.
Curiously, if I create a list of strings visible in the inspector and populate it in runtime, the list looks full and all items are there in the inspector… while if I call .count in runtime returns 0 once again, and once again I cannot access list values from script.
Not sure what I am doing wrong, any help is appreciated. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HexGrid : MonoBehaviour {
public int gridSizeX;
public int gridSizeY;
public List<HexID> Matrix = new List<HexID>();
public class HexID
{
public int ID { get; set; }
public string Name { get; set; }
public int X { get; set; }
public int Y { get; set; }
public bool Active { get; set; }
}
void Start()
{
Matrix = CreateMatrix();
GetList();
}
public List<HexID> CreateMatrix()
{
int ID = 0;
for (int y = 0; y < gridSizeY; y++)
{
for (int x = 0; x < gridSizeX; x++)
{
Matrix.Add(new HexID() { ID = ID, Name = "(" + x + ", " + y + ")", X = x, Y=y, Active = false });
MatrixX.Add(ID);
ID++;
}
}
return Matrix;
}
public List<HexID> GetList()
{
Debug.Log("Matrix size is " + Matrix.Count);
return Matrix;
}
}