Sign In Register

How can we help you today?

Start a new topic
Answered

DeviceAuthenticationRequest returns NullReferenceException

 Hi,


So I keep getting null ref every time I use device authentication. Here is my code:

 

  private void GameSparkLoginWithoutFacebook(){
    new DeviceAuthenticationRequest().Send((response) =>{
      if(response.HasErrors){
        Debug.LogError("Could not connect to GS" + response.Errors);
      }
      else{
        Debug.Log("Gamesparks login successful!");
      }
    });
  }

 

I even tried setting it to an obj (still null): 

 DeviceAuthenticationRequest myDevice = new DeviceAuthenticationRequest();

 

What am i missing here?


* I have the latest GS SDK


Thank you,


Best Answer
Hi Liam,

I fixed it by delaying the call 0.5seconds via invoke at start. The project is quite large, but the 0.5 seconds covers my needs. I appreciate your help.

Thank you,

 


Hi Lawrence,


I've double checked your code and it works fine for me, are you still getting the error ? At what point in your game are you trying to run the code ?


Thanks,

Liam




1 person likes this
I'm calling it under Awake to have it prep leaderboards while splash screen is loading. But I also tried Start and no luck. I double checked and I don't get the error if I wait till I actually request leaderboards, is there no way to authenticate sooner?

 

Hi Lawrence,


Sorry for the delayed response. Calling it in Start should be fine, not sure why this isn't working for you. Any chance you could send us over a stripped down version of the project that highlights this issue ? 


Thanks,

Liam



 



1 person likes this
Answer
Hi Liam,

I fixed it by delaying the call 0.5seconds via invoke at start. The project is quite large, but the 0.5 seconds covers my needs. I appreciate your help.

Thank you,

 

Thank you I was about to contact support but this fixed the same problem I was having! Why does this portion of code need the delay?

Hi. I have the same issue. authentication with player registration works but not Device Authentication. i want to have device authentication in my game.   


1 person likes this

Any update on this? I also tried using Invoke with a delay of 5 seconds and it works but it would be good to know what is the correct way of handling this.

@Jaayeden try calling it in Start(). Also make sure u have the latest GS SDK as required in Unity version. If u have old version of unity and new version of GS SDK there would be conflict.

Same situation here. In my case, I had IEnumrator as a return in my Start method. I tried the 0.5 seconds delay, and everything is working fine now. 

Probably, GameSpark needs time to be initialized. My GameSpark project is totally empty and the problem showed up. 


Anyway, GameSpark looks amazing, I will spend a few days doing testing.

GameSpark needs time to establish connection with the backend. 

delaying by 0.5 sec is not really a proper fix of the problem cuz the network delay is not predictable.

better use the actual service availability callback to handle this situation. 

 

void Start()
{
    GameSparks.Core.GS.GameSparksAvailable += GsServiceHandler;
}

void GsServiceHandler(bool available)
{
    if (!available)
    {
        Debug.LogError("gs service connection lost");
    }
    else
    {
        DeviceAuth();
    }
} 

 


2 people like this

Thankyou @Dylan for the solution! It works perfectly !

My architecture is the following:

- I've an Object called GameSparksManager

- The previous object has a singleton script, called GameSparksManager.cs ( following the authentication guide ).

- The same object has also GameSparksUnity.cs script ( following the authentication guide ).

- I've in GameSparksManager.cs few function to use GameSparks API in a centralised manner.

- I'm using this GameSparksManager.cs script from another object in my scene which is called GameController.cs


All method I have in GameSparksManager.cs ( getleaderboard, send an event, etc. ) works properly while are called from GameController.cs on Start() method but while calling one that internally calls to DeviceAuthenticationRequest() I got this error:


NullReferenceException: Object reference not set to an instance of an object

GameSparks.Api.Requests.DeviceAuthenticationRequest..ctor ()

GameSparksManager.LoginDeviceAuthentication (System.String username) (at Assets/Scripts/GameSparksManager.cs:104)

GameController.Start () (at Assets/Scripts/GameController.cs:66)


What's the way to work here? is this a bug? there are any workarounds?


(I've created a ticket for this: https://support.gamesparks.net/support/tickets/6656 )

Obviously the .5 seconds is an awkward fix. I fixed it with a Coroutine:


 private IEnumerator Start()
 {
 yield return new WaitUntil(() => GS.Available);

 new DeviceAuthenticationRequest().Send((response) =>
 {
 if (response.HasErrors) {
 Debug.Log(response.Errors.JSON);
 } else {
 Debug.Log("Authentication success!");
 }
 });
 }

Login to post a comment