question

luizcarlos-bs avatar image
luizcarlos-bs asked

I need to pass different commands if the user has CustomId or don't using GetAccountInfo

I'm using GetAccountInfo to get the info I need:

var requestCustomId = new GetAccountInfoRequest();
PlayFabClientAPI.GetAccountInfo(requestCustomId, OnGetAccountInfoSuccess, OnGetFailure);

After that, I need to do different things if my user has CustomId, or don't. For this, I used if...else:

        if(result.AccountInfo.CustomIdInfo.CustomId != null){
            CustomIdString.text = result.AccountInfo.CustomIdInfo.CustomId;
        }
         else { 
            CustomIdString.text = "It's null";
        }

When my user has CustomId, everything works fine. But when my user has no CustomId linked I'm getting NullReferenceException:

NullReferenceException: Object reference not set to an instance of an object
LoginWindowView.OnGetAccountInfoSuccess (PlayFab.ClientModels.GetAccountInfoResult result) (at Assets/Scripts/UI/LoginWindowView.cs:173)

the line 173 is:

if(result.AccountInfo.CustomIdInfo.CustomId != null){
                

Someone can help me??

Player Data
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Neils Shi avatar image
Neils Shi answered

According to your code, if player account doesn't have CustomId, an error will be reported like the case you provided when acessing result.AccountInfo.CustomIdInfo.CustomId. What you need to do is deleting .CustomId in if().

You can modify your code as follows:

if (result.AccountInfo.CustomIdInfo != null) {
	CustomIdString.text = result.AccountInfo.CustomIdInfo.CustomId;
} 
else { 
	CustomIdString.text = "It's null";
}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.