List becomes empty when accessed in another class
I'm decently new to both C# and Unity scripting, and am currently working on a basic procedural dungeon generator as a way to practice using the editor and all. I have one class, Room, which contains a List of doors that are available for attachment. Looking at any instance of Room in the Inspector reveals that the List has between 1 and 4 elements, as designed. However, when I call that List in another class, MapMaker, which handles the connection of Rooms, it always has zero elements. Why is this? Am I doing something wrong, not doing something I should, or are Lists subject to some limitations of which I'm simply unaware? I'll attach the problem areas of my code, and thanks in advance for any help you can provide.
public class Room : MonoBehaviour {
public string[] Tags;
public int numDoors;
public List<GameObject> connected = new List<GameObject>();
public List<GameObject> available = new List<GameObject>();
//Skip to the part of Start() where I add items to available
var temp = GetDoors();
for(int x = 0; x < temp.Length; x++)
{
if(temp[x].GetAttach() == false)
{
available.Add(temp[x].gameObject);
}
else
{
connected.Add(temp[x].gameObject);
}
}
And here's some of MapMaker:
private List<GameObject> availableDoors;
// Use this for initialization
void Start()
{
prefabs = Resources.LoadAll<GameObject>("Prefabs");
showPrefabsLength = prefabs.Length;
string[] temp = new string[] { "box" };
var hub = prefabs[1]; // Make the first room a 4-door room
var hubInstance = Instantiate(hub);
spawnCount++;
availableDoors = hub.GetComponent<Room>().available; // Gets the 'available' list of doors from hub
iterations = availableDoors.Count; //This has a value of zero. In the Inspector, hub's 'available' List is shown to have 4 elements...
In your first script change Start() to Awake(). If that helps script execution order is not setup correctly.
https://docs.unity3d.com/$$anonymous$$anual/class-ScriptExecution.html