Help with a Unity crash potentially caused by PUN
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Help with a Unity crash potentially caused by PUN
Utkarsh
2021-07-12 18:57:59
Hi! I've already posted this over at the Unity PUN forum, where I've attached crash logs as well. I don't think it's possible to attach logs here.
My game is running into a crash on scene/level load that I have identified to be caused potentially due to a change in how we are loading levels using Photon. The game on a pretty old version of PUN 2 - PUN 2.14, Photon lib: 4.1.2.17, so it does need to be upgraded.
Context for the issue: Earlier I was using PhotonNetwork.LoadLevel() with PhotonNetwork.AutomaticallySyncScene set to true. This was working fine, except for reloading levels (as is also mentioned in the documentation for this method in PUN 2 - reloading levels doesn't work on non-master clients). The level restart in my game works by actually reloading the scenes, so I changed the level loading system to use Raise Event instead. An event is raised from the master client, and then everybody does their own level loading once they receive the event.
My team noticed that this introduced an inconsistent crash during level loads in our game. More specifically, the crash seems to be happening when we try to use PhotonNetwork.InstantiateSceneObject() or PhotonNetwork.Instantiate(). In the PhotonNetwork.NetworkInstantiate() function overload that is called when an Instantiate event is received in the OnEvent() function, I added a simple log: "trying to instantiate
I can't say with 100% certainty that the cause for this is the level load change, but that is one of the only few things that I changed about how levels are loaded in the game after which this crash started happening. I have made the following observations about this crash, however:
- It is NOT a consistent crash, it seems to happen randomly.
- If a level is constantly making Unity or the standalone build crash when trying to load the level, going offline and loading it once in offline mode will stop the crashes from happening for the rest of that session. This is why I think this issue could be related to something that I might have broken in the level load in online mode.
- The crash seems to only happen on non-master clients.
- The crash seems to happen more often on lower end devices.
The only difference that I could find in the level loading done via PhotonNetwork.LoadLevel vs the Raise Event loading is the lack of pausing of online messages queue. The new Raise Event loading doesn't set PhotonNetwork.IsMessageQueueRunning to false. Could this be potentially causing the said issue? I plan to add this in our custom loading anyway, so I wanted to confirm what would be the ideal places to pause and unpause the message queue. My understanding is that the IsMessageQueueRunning bool should be set to true when a level load message is received and we start loading a level, and then as soon as the level has finished loading, set this bool back to false.
Any help on this matter is greatly appreciated.
Comments
I replied in the Unity forum:
https://forum.unity.com/threads/photon-unity-networking.101734/page-53#post-7322755