How to correctly use DontDestroyOnLoad on a gameobject with PhotonView?
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).
How to correctly use DontDestroyOnLoad on a gameobject with PhotonView?
Maeriden
2015-09-08 21:15:53
I have a scene, the main menu on which the game starts, that has a GameManager script that contains most of the game logic.
This GameManager calls DontDestroyOnLoad in its Awake and has a PhotonView used to make RPCs.
When a match starts another scene is loaded, and when it ends the main menu scene is loaded again. The GameManager Awake is called again for the newly loaded gameobject, but it's set up to destroy itself if another instance is already existing.
The problem is that the PhotonView on the second GameManager has the same ID of the first one, and it throws an exception before it is destroyed. This results in the first being destroyed by Photon and the second being destroyed by itself.
Is there a way to keep the DontDestroyOnLoad?
I'm not sure I explained it well, please ask if more details are needed.
Comments
As far as I see, the problem is that you have 2 GameManager objects at some point. It's not good, no matter with PhotonView or without.
Try to remove GameManager from menu scene and create it dynamically on room join.
The GameManager cannot be instantiated when the user enters a room since it's needed before that, but I can instantiate another object specifically for making RPCs. I'll just need to change the protection on some members.
Thank you.