场景卸载时出现 MissingReferenceException
MissingReferenceException at scene unload time
我想在当前 运行 场景卸载后存储我的数据。为此,我编写了以下代码:
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);
}
}
}
但是当调用 OnDisable 方法时控制台给我以下错误:
我已经在项目设置中设置了脚本的执行顺序,为什么会出现这种错误?
目标: 基本上我想保存当前的游戏数据,这样当玩家 return 回来时他可以再次从他离开游戏的地方开始。
我有一个建议,你可以在每次玩家移动时保存,而不是在场景卸载时保存,这样即使游戏崩溃你也可以从玩家最后一次移动时恢复。
这个场景卸载,会自动发生还是用户会做任何动作(比如点击按钮)?如果它是玩家事件,你应该把你的保存代码放在那个按钮上,因为据我所知,OnDisable 行为将在你的对象被销毁后被调用,使你无法存储它们的数据。
这个问题花了一些时间,现在我可以弄清楚这个问题了。我不知道有多少解决方案是正确的,但至少现在对我有用。
我在这里讨论这个是因为有些成员从中得到了一些提示。
在游戏加载时,我用所有单元格的脚本填满了一个列表。网格的每个单元格都附有一个脚本 CellInformation。所以我准备了所有这些单元格脚本的列表。
在游戏禁用时,我从这些脚本中获取值并存储到本地存储中。我可以访问脚本,但不能访问列表中的游戏对象,因为它们已经被销毁了。
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);
}
}
我希望这能给其他程序员一些提示。
我想在当前 运行 场景卸载后存储我的数据。为此,我编写了以下代码:
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);
}
}
}
但是当调用 OnDisable 方法时控制台给我以下错误:
我已经在项目设置中设置了脚本的执行顺序,为什么会出现这种错误?
目标: 基本上我想保存当前的游戏数据,这样当玩家 return 回来时他可以再次从他离开游戏的地方开始。
我有一个建议,你可以在每次玩家移动时保存,而不是在场景卸载时保存,这样即使游戏崩溃你也可以从玩家最后一次移动时恢复。
这个场景卸载,会自动发生还是用户会做任何动作(比如点击按钮)?如果它是玩家事件,你应该把你的保存代码放在那个按钮上,因为据我所知,OnDisable 行为将在你的对象被销毁后被调用,使你无法存储它们的数据。
这个问题花了一些时间,现在我可以弄清楚这个问题了。我不知道有多少解决方案是正确的,但至少现在对我有用。
我在这里讨论这个是因为有些成员从中得到了一些提示。
在游戏加载时,我用所有单元格的脚本填满了一个列表。网格的每个单元格都附有一个脚本 CellInformation。所以我准备了所有这些单元格脚本的列表。
在游戏禁用时,我从这些脚本中获取值并存储到本地存储中。我可以访问脚本,但不能访问列表中的游戏对象,因为它们已经被销毁了。
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);
}
}
我希望这能给其他程序员一些提示。