Unity MySelf.IsSpectator Exception

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

ersindemir
Posts: 26
Joined: 30 Apr 2016, 07:23
Location: Turkey
Contact:

Unity MySelf.IsSpectator Exception

Postby ersindemir » 06 Feb 2017, 09:32

Sometimes I got this exception on Unity client side, when I use this function ; (It is so hard to reproduce !)

bool isSpectator = smartfox.MySelf.IsSpectator ;

NullReferenceException : Object reference not set to an instance of an object
at Sfs2X.Entities.SFSUser.GetPlayerId(Sfs2X.Entities.SFSUser)
at Sfs2X.Entities.SFSUser.get_PlayerId(Sfs2X.Entities.SFSUser)
at Sfs2X.Entities.SFSUser.get_IsPlayer(Sfs2X.Entities.SFSUser)
at Sfs2X.Entities.SFSUser.get_IsSpectator(Sfs2X.Entities.SFSUser)

Unity v : 5.5.0f3
Smartfox backend v : 2.12.0
Smartfox client v : 1.6.6
User avatar
Lapo
Site Admin
Posts: 20044
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity MySelf.IsSpectator Exception

Postby Lapo » 07 Feb 2017, 09:19

Hi,
this is likely due to incorrect Room logic.
You must check to be at least joined in a Room if you want to query the user's status in that Room (e.g. is the player a spectator etc...)

If your client uses one room at a time (no multi-room) simply add this check before reading the isSpectator property:

Code: Select all

if (sfs.LastJoinedRoom != null)
{
   // check user status here...
}


This is because isSpectator and isPlayer assume you're at least joined in a Room (the one referenced by lastJoinedRoom property) and if a join error occurred or the player is simply not connected to any Room, this will fail.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
ersindemir
Posts: 26
Joined: 30 Apr 2016, 07:23
Location: Turkey
Contact:

Re: Unity MySelf.IsSpectator Exception

Postby ersindemir » 07 Feb 2017, 09:26

After SFSEvent.ROOM_JOIN event is fire from smartfox , I got this exception. lastJoinedRoom can not be null ( Again it is so hard to reproduce)
I saw this exception on crashlytics.
User avatar
Lapo
Site Admin
Posts: 20044
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Unity MySelf.IsSpectator Exception

Postby Lapo » 07 Feb 2017, 09:53

Since the error appears only in rare cases I would recommend to revise your code and make sure that join errors are handled correctly, otherwise the game logic will proceed to execute code that fails because your application is in the wrong state.

Another option is that the user has left the Room, but not yet joined another one. In any case lastJoinedRoom will be null.

We have taken a closer look at the API's code and there's no other way to cause the NRE other than the lastJoinedRoom being Null.
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 1 guest