Reputation: 4312
I want to store my data when current running scene was unloaded. For this purpose I have written following code:
void OnDisable ()
{
BackUpPuzzleData();
}
public void BackUpPuzzleData ()
{
if (DataStorage.RetrievePuzzleStatus (difficultyLevel, puzzleId) == Constants.PUZZLE_NOT_OPENED
&& DataStorage.RetrievePuzzleStatus (difficultyLevel, puzzleId) != Constants.PUZZLE_COMPLETED)
DataStorage.StorePuzzleStatus (difficultyLevel, puzzleId, Constants.PUZZLE_RUNNING);
if (DataStorage.RetrievePuzzleStatus (difficultyLevel, puzzleId) == Constants.PUZZLE_RUNNING)
StorePuzzleData ();
}
private void StorePuzzleData ()
{
DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime);
foreach (Transform cell in gridTransform) {
CellInformation cellInfo = cell.GetComponent<CellInformation> ();
if (cellInfo != null) {
CellStorage.StorePuzzleCellNumber (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.number);
CellStorage.StorePuzzleCellColor (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.CellColor);
CellStorage.StorePuzzleCellDisplayColor (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.CellDisplayColor);
}
}
}
But when OnDisable method gets called at that time Console giving me following error:
I have already set execution order of scrip in project settings then why I am getting this kind of error?
GOAL: Basically I want to save current game play data so when game player return back he can again start from which he left game.
Upvotes: 0
Views: 89
Reputation: 4312
Passing some time with this problem, now I can able to figure out this problem. I don't know how much solution was correct but at least now working for me.
Here I am discussing this because some members get some hint from this.
At game load time, I filled up one list with all cell's script. Each cell of grid has one script attached CellInformation. So I prepare list of all these cells script.
At game disable time, I fetched value from these scripts and store into local storage. I can able to access scripts but not game objects from list because they were already destroyed.
void OnDisable ()
{
StorePuzzleData ();
}
private void StorePuzzleData ()
{
DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime);
foreach (CellInformation cellInfo in cellInfoList) {
CellStorage.StorePuzzleCellNumber (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.number);
CellStorage.StorePuzzleCellColor (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.CellColor);
CellStorage.StorePuzzleCellDisplayColor (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.CellDisplayColor);
CellStorage.StorePuzzleCellGroupComplete (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.IsGroupComplete ? 1 : 0);
}
}
I hope this will give some hints to other programmers.
Upvotes: 0
Reputation: 571
I have a suggestion, instead of saving when scene unload you could save everytime the player makes a move, this way even if the game crashes you can recover from the last time the player made a move.
This scene unloading, will occur automatically or will the user make any action (like clicking a button)? If it is a player event, you should put your save code on that button, because, as far as I could research, the OnDisable behaviour will be called after your objects gets destroyed, making it impossible for you to store their data.
Upvotes: 1