Saurabh
Saurabh

Reputation: 61

NullReferenceException on Public Method Unity3D

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

Upvotes: 0

Views: 270

Answers (1)

Saurabh
Saurabh

Reputation: 61

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

Now it is like

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

} }

Thanks

Upvotes: 2

Related Questions