a error, but when i click it 2nd time, all work fine.
Also when seraching a GitHub i had see this answer “Before you can call WriteSaveGame, you need to wait until the read has finished since it is asynchronous. So a simplistic approach (you should add some error handling) is to add the Write to the callback in readBinary().”, but i got no idea how to fix the problem.
void ReadSaveGame (string filename, Action<SavedGameRequestStatus, ISavedGameMetadata> callback) {
// Check if signed in
if (dailyreward.check ) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution (filename,
DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime,
callback);
}
}
// Write the save game
void WriteSaveGame (ISavedGameMetadata game, byte [] savedData, Action<SavedGameRequestStatus, ISavedGameMetadata> callback) {
// Check if signed in
if (dailyreward.check == true) {
SavedGameMetadataUpdate updatedMetadata = new SavedGameMetadataUpdate.Builder ()
.WithUpdatedPlayedTime (TimeSpan.FromMinutes (game.TotalTimePlayed.Minutes + 1))
.WithUpdatedDescription ("Saved at: " + DateTime.Now)
.Build ();
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.CommitUpdate (game, updatedMetadata, savedData, callback);
}
}
// Save the game
public void SaveGame ()
{
// Check if signed in
if (dailyreward.check == true) {
// Save game callback
Action<SavedGameRequestStatus, ISavedGameMetadata> writeCallback =
(SavedGameRequestStatus status, ISavedGameMetadata game) => {
};
// Read binary callback
Action<SavedGameRequestStatus, byte[]> readBinaryCallback =
(SavedGameRequestStatus status, byte[] data) => {
};
// Read game callback
Action<SavedGameRequestStatus, ISavedGameMetadata> readCallback =
(SavedGameRequestStatus status, ISavedGameMetadata game) => {
// Check if read was successful
if (status == SavedGameRequestStatus.Success) {
currentGame = game;
PlayGamesPlatform.Instance.SavedGame.ReadBinaryData (game, readBinaryCallback);
}
};
// Create new save data
SaveData saveData = new SaveData
{
// saving vartiables here
};
ReadSaveGame ("MySaveGame", readCallback);
WriteSaveGame (currentGame, SaveData.ToBytes (saveData), writeCallback);
}
}
I get this error: NullReferenceException: Object reference not set to an instance of an object CloudSaver.ReadSaveGame (System.String filename, System.Action`2 callback) (at Assets/scripts/CloudSaver.cs:160) CloudSaver.SaveGame () (at Assets/scripts/CloudSaver.cs:303) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()