How to correctly use DontDestroyOnLoad on a gameobject with PhotonView?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

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).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

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

vadim
2015-09-09 13:32:53

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.

Maeriden
2015-09-09 16:46:07

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.

Back to top