Situation as follows:
-
Modified Roll-a-Ball tutorial game to accept serialized saving & loading. Finally got it to work! Code is [here][1]. It was meant to save player position, score, and track which collectible objects had been picked up.
-
However “load” can only be used once (without “Reloading”), after which collectibles start randomly disappearing. I stuck a ‘print’ line into the initialization, and the console shows that after a few clicks, only “Pickup 0” through “Pickup 2” are initializing. “Pickup 0,” 8, and 9 remain active and all the others disappear.
public void Restart() { //Future: Include warning message about losing unsaved progress SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); GameControl.Instance.InitializeNR(); GameControl.Instance.Count = 0; print("Level " + SceneManager.GetActiveScene().buildIndex + " reloaded"); }
If the player uses Restart() - also called from the pause menu - Load proceeds to work as normal (for the first use only). Here’s the kicker: calling the SceneManager… line to reload in the LoadGame() method causes the player to snap back to the scene origin and all collectibles to reset their Active status to true (regardless of whether they should actually be active or not). The only thing that is preserved from LoadGame() is the player’s score. The same is true if the Restart() here is called from LoadGame().
So basically the glitch is, either Load will gradually stop initializing my Collectibles, or SceneManager will override all my savegame’s data (except the player’s score). I managed to muddle my way through this far, but I don’t even know where to begin fixing this one.
EDIT:
Think I figured out the root of my problem, still not sure how to solve. In GameControl.cs, my initialization calls “GameObject.Find…”
public void InitializeNR()
{
var i = 0;
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Pick-up"))
{
go.SetActive(true);
print("Pickup " + i + " initialized");
Pickups *= go;*
Collected = false;
i++;
}
}
As I have since learned, GO.Find… functions don’t locate inactive objects. The trouble is, I used that call specifically because loading / reloading the scene resulted in “Missing object” reference errors in the Pickups[] array, from collectibles that were destroyed/reloaded.
I suppose my question then becomes, how do I circumvent this error while avoiding the use of Find? I’ve seen suggestions about doing an If check for null references, but while that bypasses the error, it doesn’t fix the missing reference.
_*[1]: http://answers.unity3d.com/questions/1211387/storing-the-state-of-a-collectible-in-save-file.html*_