简体   繁体   中英

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

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

Now it is like

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

} }

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM