After several days of coding I’ve got into a weird problem. Two arrays turn to be null while they actually have elements (according to Inspector). I reduced the scripts to the essential parts. (there are 2 different scripts – don’t know how to edit that here)
public class GenerateSpells : MonoBehaviour {
public GameObject[] spells;
private Object[] resourcesLoaded;
private int index = 0;
// Use this for initialization
void Start () {
index = 0;
resourcesLoaded = Resources.LoadAll ("", typeof(GameObject));
foreach (Object o in resourcesLoaded) {
if (o.name.Length > 6 && o.name.Substring (0, 6) == "spell_") {
spells[index] = (GameObject)o;
index++;
}
}
}
public class SetupInventory : MonoBehaviour {
public GameObject[] Inventory;
public GameObject[] spells = new GameObject[100];
private int index = 1;
void Start () {
spells = Camera.main.GetComponent<GenerateSpells> ().spells;
Debug.Log (spells.Length); //shows 14
Debug.Log (Camera.main.GetComponent<GenerateSpells> ().spells.Length); //shows 14
Debug.Log (spells [0]); //shows Null
Debug.Log (Camera.main.GetComponent<GenerateSpells> ().spells [0]); //shows Null
}