Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug RateLimiting causing silent exeption which causes AddPlayerScoreAsync to fail.

Discussion in 'Leaderboards' started by Chris_Webb, Mar 26, 2023.

  1. Chris_Webb

    Chris_Webb

    Joined:
    Mar 18, 2014
    Posts:
    128
    Hello,

    When executing AddPlayerScoreAsync via Task.Run, Unity throws an exception:

    Code (CSharp):
    1. Task.Run(async () => { await LeaderboardsService.Instance.AddPlayerScoreAsync(leaderboardID, score); });
    "UnityEngine.UnityException: get_unscaledTime can only be called from the main thread."

    Originating from

    Code (CSharp):
    1. public bool RateLimited => Time.unscaledTime < m_rateLimitUntilUnscaledTime;
    As a result, the submission is silently swallowed and never occurs.

    [EDIT]
    Just noticed you can happily just call async methods in Unity, as long as you don't need the return immediately (which I don't). So that works perfectly fine. Sorry for the confusion!

    Thanks,
    Chris
     
    Last edited: Mar 26, 2023
    Unity_AndyP likes this.
  2. neal-wang

    neal-wang

    Unity Technologies

    Joined:
    Feb 23, 2021
    Posts:
    7
    Hi Chris,

    Great job! Just to provide a bit more information, the exception is actually intentional. Web requests must be made from the main thread, which is why this exception occurs.

    Thank you,
    Neal