简体   繁体   English

公共方法Unity3D上的NullReferenceException

[英]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. 我试图在相同的脚本AutoSaveData()中调用一个公共方法,这给了我Null引用异常。 I have also added DontDestroyOnLoad to this script so that the game object persists between scenes. 我还向此脚本添加了DontDestroyOnLoad,以便游戏对象在场景之间持久存在。 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. 我通过在AutoSaveData()函数中添加条件来解决此问题。

Now it is like 现在就像

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

} } }}

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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