question

Paul avatar image
Paul asked

"request.Statistics[0].Value" leads to "NullReferenceException: Object reference not set to an instance of an object"?

Hi, sorry i dont konw a better thread name.

before i posted this read multiple threads in here and i also tryed to find a solution via google.
although i could find multiple threads with a null referenceexepction problem, none of the provided solutions could help me with my problem.

I hope this way someone might be able to explain to me what the hell is going on :/

So i basically want to add some player stats everytime some things happen and that is what iam doing:

public void ChangeStats() {

var request = new UpdatePlayerStatisticsRequest();

request.Statistics[0].Value++;

PlayFabClientAPI.UpdatePlayerStatistics(request, updatestat, errorCallback);

}

and the failure iam getting is:
"NullReferenceException: Object reference not set to an instance of an object PlayFabNews.ChangeStats () (at Assets/Scripts/PlayFabNews.cs:40)"

Line 40 in playfabnews is exactly the the line above "request.Statistics[0].Value++;"

so what do i have to instantiate here?

thanks for any help!

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

·
brendan avatar image
brendan answered

Well first, I highly recommend not turning on the ability for the client to update statistics. The reason that's off by default for all titles is because letting the client update stats directly makes it trivially easy for a hacker to post any stat he wants to into your game. That's also why the virtual currency Client API calls are off by default.

That said, the issue you're running into is that when you create a request object, it's empty by default. A List, like Statistics, doesn't have elements already in it, so trying to dereference element 0 in the list will fail. What you'll want to do is add each stat to the request using a call like this:

request.Statistics.Add(newStatisticUpdate { StatisticName= nameOfTheStat, Value= valueOfTheStat});
3 comments
10 |1200

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

Paul avatar image Paul commented ·

appreciate your answer! next time i run into this, i will do it that way. by now i did go a (very) long way around this :D

sadly i have no clue how to update the player stats without calling it from the client?

i watch as much tutorials as i can, but didnt acquire the necessary knowledge so far tho

0 Likes 0 ·
brendan avatar image brendan Paul commented ·

There's an example in the basic Cloud Script which is added as Revision 1 to all new titles. Have a look at the completedLevel handler. Also, there are tutorials on using Cloud Script here: https://api.playfab.com/docs/tutorials#landing-automation

0 Likes 0 ·
Paul avatar image Paul brendan commented ·

will have a loook at it, thanks again :)

0 Likes 0 ·

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.