Trouble with SceneMananger.sceneLoaded and passing dictionaries
Pardon the very ugly code. If I can get this to work I will try to go back and make it more readable and expandable.
Basically I am trying to create a dictionary containing an id number and a gameObject for each of my different collectible types upon loading a level (What GenerateItemDictionaries() does here).
Then, upon picking up an item, I want to remove it from the dictionary and a separate list of id's that I'm using to save the level data elsewhere.
The problem is that it seems like immediately after I run GenerateItemDictionaries(), where honeyDict is created, my OnItemPickup() function gets honeyDict == null.
This is run in a separate class using SceneManager.sceneLoaded += OnSceneLoad;
private void OnSceneLoad(Scene scene, LoadSceneMode mode)
{
levelDataListManager.hasGeneratedItemDictionaries = false;
GenerateUI();
ResetPlayerProperites();
SaveOnSceneLoad();
levelDataListManager.GenerateItemDictionarys();
levelDataListManager.GenerateLevel();
}
These are both in my LevelDataListManager class.
public void GenerateItemDictionarys()
{
Debug.Log("Generating Dictionaries");
honeyDict = new Dictionary<int, GameObject>();
int i = 0;
foreach (GameObject honeyObject in GameObject.FindGameObjectsWithTag(itemTags[0]))
{
honeyDict.Add(i, honeyObject);
i++;
}
keyDict = new Dictionary<int, GameObject>();
int j = 0;
foreach (GameObject keyObject in GameObject.FindGameObjectsWithTag(itemTags[1]))
{
keyDict.Add(j, keyObject);
j++;
}
if (honeyDict == null)
{
Debug.Log("honeyDict null in GenerateItemDictionaries");
}
hasGeneratedItemDictionaries = true;
}
public void OnItemPickup(int itemTypeIDNum, GameObject item)
{
switch (itemTypeIDNum)
{
case 0:
if (!hasGeneratedItemDictionaries)
{
Debug.Log("hasn't generated the dicitonaries dingus");
}
if (honeyDict == null)
{
Debug.Log("honeyDict null in switch");
}
int honeyKey = FindGameObjectKey(item, honeyDict);
honeyDict.Remove(honeyKey);
currentLevel.honeyArray.Remove(honeyKey);
break;
case 1:
int keyKey = FindGameObjectKey(item, keyDict);
keyDict.Remove(keyKey);
currentLevel.keyArray.Remove(keyKey);
break;
}
}
The only places where the value of hasGeneratedItemDictionaries are changed are in GenerateItemDictionaries() and inside OnSceneLoad().
So, if I go by the order things run inside OnSceneLoad(), hasGeneratedItemDictionaries is set to false, and then after OnSceneLoad() runs GenerateItemDictionaries(), hasGeneratedItemDictionaries is set to true. It couldn't be set back to false until another scene loads, and GenerateItemDictionaries() is only called inside OnSceneLoad(). So I am very confused as to how GenerateItemDictionaries() sets hasGeneratedItemDictionaries to true, and then without any scene load in between it is back to false.
If I toss this in:
void Update()
{
if (!hasGeneratedItemDictionaries)
{
Debug.Log("!hasGeneratedItemDictionaries");
}
}
It does not trigger at any point. I am not running OnItemPickup on the same frame the scene loads, either. That triggers after several frames of me walking over to a collectable and hitting it's trigger. I have been working at this one for hours and my eyes are starting to glaze over any code. Any help is very much appreciated!!