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! Your search result can be found below. Plus, we offer support via these channels:

Try Our

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.

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?

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.


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.

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