AddPlayerScoreAsync returns null

var scoreResponse = await LeaderboardsService.Instance.AddPlayerScoreAsync( LeaderboardId, 22 );
Gives me
scoreResponse == null

Now if I change LeaderboardId to something bogus I Get
HttpException`1: (404) HTTP/1.1 404 Not Found

thus LeaderboardId is correct so why does it return null?

My first thought is I’m not signed in but I do
await AuthenticationService.Instance.SignInAnonymouslyAsync();
print( “Sign in anonymously succeeded! Player Id:” + AuthenticationService.Instance.PlayerId );

givesme
Player Id:MiX9qkotw38kbLTk1PO00n8lTe8w
thus I assume that worked

So whats up?
Cheers Zed

Hey Zed,

Thanks for reaching out.

There’s a Leaderboards SDK Sample that covers most of the basic use cases here, which includes service initialization, authenticating and serializing response objects as well as links to creating your leaderboard on the Unity Cloud Dashboard.

If you still have issues can you post a complete sample of what you’re doing and I’ll have a look into it?

Thanks,

Jamie

1 Like

Cheers mate, yeah thats what I’m doing

    public async void AddScore()
    {
        var scoreResponse = await LeaderboardsService.Instance.AddPlayerScoreAsync(LeaderboardId, 102);
        Debug.Log(JsonConvert.SerializeObject(scoreResponse));
    }

My problem is var scoreResponse == null, i.e it return null

According to the docs, it only should return Task<LeaderboardEntry>
https://docs.unity3d.com/Packages/com.unity.services.leaderboards@2.0/api/Unity.Services.Leaderboards.ILeaderboardsService.AddPlayerScoreAsync.html

Though of course perhaps the doc’s forgot to mention that it can return null if something went wrong, so my question is what can cause it to return that?

EDIT: To make it clear I am doing the SDK example thus I assume the problem lies elsewhere, I’ve checked everything but are not sure where the problem lies

Hmmm Jsonconvert? It returns a Task which isn’t a json string. :wink:

Try:
Debug.Log(scoreResponse);

Be sure to enclose these calls in try/catch exception handling, even while doing tests. Service calls WILL fail one way or another, ie user loses Internet connection.

2 Likes

Cheers mate, I’m not 100% sure what you mean WRT json string
like I said scoreResponse == null ← that is the problem, why is it returning null, i.e. whats causing it to fail?

the above code is from the Unity Script Example.
I assume the example code is correct

My brain is failing me I’ve been stuck on this 2 days now, and are getting dumber each minute.
Ideally I would just roll my own online database (as I’ve done many times over the years in SQL but due to lack of budget I don’t have access to that, I even couldn’t afford the amazon free database trial yesterday, haha )

EDIT: Wait a sec, now its not null, huh!
I’ll dig into this some more

I agree with CodeSmile, wrapping it up in a try-catch block could provide more insights and could be expanded to add some resiliency down the line.

Even just something like this for now:

public async void AddScore()
{
    try
    {
        var scoreResponse = await LeaderboardsService.Instance.AddPlayerScoreAsync(LeaderboardId, 102);
        if (scoreResponse == null)
        {
            // To confirm I think it's much more likely to be an exception than ever get to this line
            Debug.LogError("Score response is null.");
        }
        else
        {
            Debug.Log(JsonConvert.SerializeObject(scoreResponse));
        }
    }
    catch (Exception ex)
    {
        Debug.LogError($"An error occurred: {ex.Message}");
    }
}

Regarding the null responses, I’ve reviewed the SDK implementation, and it seems that every response or failure from the leaderboards service should be encapsulated in a response object.

Edit - Glad to see your edit!

2 Likes

Hey thanks for going above the call of duty.
I’m not sure why it was responding with null, I was testing with something like

if ( scoreResponse == null )
{
GLOBALS.LogError( "Null here" );
}

But if you say that’s impossible, well I suppose you’re right.

Yes good advice with the try/catch thing, even though I’m quite a big logger, I rarely use try/catches. Though I suppose here is a good place as like CodeSmile said, Internet is not 100% guaranteed.
I’ll have another look tomorrow once I’ve rested. In trying to solve this I updated my Steamworks Version and now my old code doesn’t work as some of the functions I was using don’t work now eg SignInWithSteamAsync()

1 Like

FWIW I did sorta implement it here,
Though not fully as
#1 I couldn’t get the authentication to work fully ( even though its installed I can’t seem to update it, but all good, never mind, also I wanna let the person write their own sig each time )
#2 I’m wanting to do more with the leaderboard eg perhaps top 5 scores per person on each of the 4 scenes. I just think SQL is a better fit for my purpose as it’s more flexible (yes it’s more difficult to use but I’ve used that a lot in the past, I had a horror film site like IMDB a long time ago, so know my way around DB languages)
If you’re curious there’s a demo here

Cheers for the Speedy help Jamie and CodeSmile

For #1, what do you mean exactly?
Authentication allows you to bring your own sign-in: Custom ID sign-in

For #2, could you give more details?
You can get a range around the player: Method GetPlayerRangeAsync | Leaderboards | 2.0.0

or any scores overall: Method GetScoresAsync | Leaderboards | 2.0.0

Cheers!