0
void Update () {

    if (isSaveNeeded){
        AutoSaveData();
        isSaveNeeded = false;
    }

    string status;
    if (Social.localUser.authenticated) {
        status = "Authenticated ";

        if (isLoaded == false){
            LoadAutoSave();
            isLoaded = true;
        }
    }
    else {
        status = "Not Authenticated";
    }

    statusToPass = status + " " + mMsg;
}

public void OnSignIn() {
    if (Social.localUser.authenticated) {
        PlayGamesPlatform.Instance.SignOut();
    }
    else {
        PlayGamesPlatform.Instance.Authenticate(mAuthCallback, false);

    }
}


public void LoadData() {
    ((PlayGamesPlatform)Social.Active).SavedGame.ShowSelectSavedGameUI("Select Saved Game to Load",
                                                                       10,
                                                                       false,
                                                                       true,
                                                                       SaveGameSelectedForRead);
}

public void SaveData() {
    ((PlayGamesPlatform)Social.Active).SavedGame.ShowSelectSavedGameUI("Save Game Progress",
                                                                       10,
                                                                       true,
                                                                       false,
                                                                       SaveGameSelectedForWrite);

}

public void AutoSaveData() {
    ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution(autoSaveFileName,
                                                                                     DataSource.ReadCacheOrNetwork,
                                                                                     ConflictResolutionStrategy.UseLongestPlaytime,
                                                                                     SavedGameOpenedForWrite);


}

public void LoadAutoSave() {
    ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution(autoSaveFileName,
                                                                                     DataSource.ReadCacheOrNetwork,
                                                                                     ConflictResolutionStrategy.UseLongestPlaytime,
                                                                                     SavedGameOpenedForRead);

} 

I am trying to call a public method in the same script called AutoSaveData() and it is giving me Null reference exception. I have also added DontDestroyOnLoad to this script so that the game object persists between scenes. I have been looking into it for some hours now and couldn't figure out the cause for it. It might be a simple mistake on my part but as I am new to coding, probably I am not able to figure it out. Thanks

6
  • You don't need to look at the code, you need to debug the code. Commented Jul 24, 2015 at 18:29
  • I meant that only that I was trying to debug the code and was trying all sorts of solutions from over the web. Commented Jul 24, 2015 at 18:42
  • A NullReferenceException is not like a nullPointer, doesn't matter DontDestroyOnLoad because your problem is not at runtime but compilation time if it is really a NullReference. Probably you forgot to declare namespace for some class inside your AutoSaveData(), please submit your full trace from console or track it untill you find your real problem because it cant be AutoSaveData method once it is on same script and is not a reserved word by C# or Unity. Commented Jul 24, 2015 at 18:53
  • @Frohlich What i am doing with the above script is that when the game over happens, i set the variable isSaveNeeded to True, which triggers the AutoSaveData() function. This is where i start getting the NullReferenceException. Here is the full error :- NullReferenceException: Object reference not set to an instance of an object AutoSave.AutoSaveData () (at Assets/Scripts/Managers/AutoSave.cs:189) AutoSave.Update () (at Assets/Scripts/Managers/AutoSave.cs:112) Here 112 line is AutoSaveData() trigger line and 189 is the function AutoSaveData() itself from ((PlayGamesPlatform)....... Commented Jul 24, 2015 at 19:03
  • Yeah, sorry I mistaken the exception type, forgot unity's Object not set to an instance of object exception. Where is your "autoSaveFileName" declared and when you initialize/instanciate it? Commented Jul 24, 2015 at 19:57

1 Answer 1

2

I fixed it by adding condition to my AutoSaveData() function.

Now it is like

if(Social.localuser.authenticated){ AutoSaveData(){

} }

Thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.