question

jorgemateo avatar image
jorgemateo asked

Error when trying to store a local variable

I am trying to store the data I get from a request in a local variable, when its stored it doesn't give an error, but after that, when i try to use it gives me the error of local variable not assigned.

Here is the code:

List<PlayFab.AdminModels.StoreItem> storeList;
        
        var request = new PlayFab.AdminModels.GetStoreItemsRequest
        {
            StoreId = "Store",
        };

        PlayFabAdminAPI.GetStoreItems(request, result =>
        {
            storeList = result.Store;
        }, error =>
        {
        });

        foreach(var i in storeList)
        {
            
        }
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

·
Seth Du avatar image
Seth Du answered

storeList is local variable and you may need to use it in the callback of GetStoreItems like:

 PlayFabAdminAPI.GetStoreItems(request, result => 
{ 
storeList = result.Store; 
foreach (var i in storeList) 
{ 
} 
}, error => 
{ 
});

Even you define storeList as a global variable, if you put it out of callback result, it is not guaranteed to be able to retrieve the result you get from GetStoreItems API because PlayFab Unity API is asynchronous.

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.