EditorApplication.isPlaying doesn't work as intended

Hi Photon Developers,

We encountera strange behaviour while integrating Photon into our latest project. In the past we haven't had an issue, but for what ever reason we kept getting, NullReferenceException in PhotonNetworkPart.cs in the AddCallbackTarget, specifically the NetworkingClient was null. It turned out that for whatever reason our new project setup (or possibly because of the latest version of Unity: 2019.4.1), the line
if (!EditorApplication.isPlaying) return;
, was returning false when entering play mode.
After digging around , this forum post pointed us in the right direction:

https://answers.unity.com/questions/58122/detect-if-game-is-in-play-or-edit-mode.html

basically there is a high chance that when switching to play mode in the editor that EditorApplication.isPlaying will return false, hence Photon will not initialize properly and cause null reference exceptions.

Our work around was to change that line of code (It is in the static constructor of PhotonNetwork) to
if (!EditorApplication.isPlayingOrWillChangePlaymode) return;
Now everything works correctly.

This seems like a bug (albeit one that happens very rarely). Can the team confirm, and also confirm if our work around is viable and will not cause other issue down the line.


Thanks

Comments

  • Thanks a lot for this comprehensive bug report and the fitting fix for it.
    We noticed there is something wrong in some cases but could not reproduce this. As you noticed, not all Unity versions have this on every enter playmode.

    So, yes, this makes a lot of sense and the fix you applied is already checked in for the PUN v2.20 update.

    Much appreciated!