Instantiate issues when player disconnects and reconnects

Options
Hi

I'm getting a crash when player leaves and then rejoins a room.
ArgumentException: The Object you want to instantiate is null.
  at UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.Object.Instantiate[T] (T original) [0x00000] in <00000000000000000000000000000000>:0 
  at UnitPool.GetObjectForType (System.String cardAsString, System.Boolean onlyPooled) [0x00000] in <00000000000000000000000000000000>:0 
  at UnitPool.Instantiate (System.String prefabId, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) [0x00000] in <00000000000000000000000000000000>:0 
  at Photon.Pun.PhotonNetwork.NetworkInstantiate (Photon.Pun.InstantiateParameters parameters, System.Boolean sceneObject, System.Boolean instantiateEvent) [0x00000] in <00000000000000000000000000000000>:0 
  at Photon.Pun.PhotonNetwork.NetworkInstantiate (ExitGames.Client.Photon.Hashtable networkEvent, Photon.Realtime.Player creator) [0x00000] in <00000000000000000000000000000000>:0

Let me explain my setup.

It's a 2 player game where players place units that battle each other. I have CleanupCacheOnLeave set to false because I don't want the units to disappear if a player disconnects. Instead, it transfers the ownership of the unit instance to the remaining player so it can go on fighting. This part of it all seems to be working well.

I have 2 prefab pools set up - not sure if this is a good idea or not. When I need to instantiate an object I set the current pool and then call instantiate
PhotonNetwork.PrefabPool = AssetLoader.Instance.unitPool;
 go = PhotonNetwork.Instantiate(cardAsString, pos, Quaternion.identity,0, myCustomInitData);

and
PhotonNetwork.PrefabPool = AssetLoader.Instance.castlePool;
sectionInstance = PhotonNetwork.Instantiate(cardAsString, Vector3.zero, Quaternion.identity,0, myCustomInitData);

If a player disconnects and then reconnects while the battle is still active I call
PhotonNetwork.RejoinRoom(SaveManager.Instance.DownloadedActiveBattleData.RoomName);

I can see that it successfully rejoins the room and then it crashes, so I guess I am after some info on what PhotonNetwork.Instantiate would do in this scenario. Does it identify everything that was created with PhotonNetwork.Instantiate that exists in the room and attempt to recreate it for the rejoining player? How would it handle the multiple pools? If the player has quit the app completely and then tried to rejoin the room then unity would have deleted the prefab pools - do I need to recreate the pools whenever someone reconnects with Photon? How do others handle this scenario?

Lots of questions. Any help would be appreciated.

Thanks

Comments

  • BigGameCo
    Options
    would it be better to use InstantiateSceneObject in this scenario?
  • BigGameCo
    Options
    I think I have managed to fix the some of the instantiate null errors by doing away with the multiple pools and just pooling the prefabs using the example here: https://forum.unity.com/threads/solved-photon-instantiating-prefabs-without-putting-them-in-a-resources-folder.293853/

    However, I just keep hitting brick walls and I'm worried that I might be over complicating it. Do photon have any demo projects that show how to deal with this scenario, where a player wants to resume a game after completely closing app and then reopening it? This is for iOS by the way.

    Thanks
  • BigGameCo
    Options
    Also worth noting, with my current approach this is what happens:

    2 players enter battle
    Player A places some units
    If Player A disconnects, player B takes control of the units and they keep attacking
    If Player A reconnects, when they enter the room, their units reinstantiate at the place where they were originally placed and do not appear to be fully initialised properly. The instances also appear to have their ownership given back to Player A.

    I assumed they would be reinstantiated at wherever they had moved to and that player B would retain ownership. Is there anyway to achieve this?

    Thanks again
  • BigGameCo
    Options
    InstantiateSceneObject seems to be getting me closer to my goal. Would love to hear from someone at Photon to know if this is the right approach and if there are any existing demos of that show how to approach this scenario - I would have thought it would be quite a common problem.