Photonview SceneViewId bug on prefabs

Options
Hello. Found a bug in PUN that got me scratching my head for a while.

The bug:
We have partially generated word that contains multiple Room prefabs that contain PhotonView component. Room also has child objects that contain PhotonView component. Rooms get instantiated via PhotonNetwork.Instantiate(). In my research, the nested prefabs structure did not make a difference.
Problem was that randomly some of the Instantiated Rooms would cause bunch of PhotonView ID duplicate found: and InvalidOperationException: Duplicate key errors, which made no sense, since by testing I noticed that only 4 rooms out of 44 was causing this issue.

By digging I found out that Unity had saved (probably on the runtime) the prefab's (unexposed) sceneViewId on the PhotonView component. This caused the PhotonView component to automatically change it's ViewID to this saved value in PhotonView.cs Awake() function.
By doing this, Photon will automatically force other objects with that ViewID to be destroyed, which in our case was core scene objects.

The fix:
Minor change to PhotonViewHandler.cs OnHierarchyChanged() function. Starting the line 63
Old:
if (view.ViewID != 0)
{
   view.ViewID = 0;
   EditorUtility.SetDirty(view);
}

With scene view zeroing
if (view.ViewID != 0 || view.sceneViewId != 0)
{
    view.ViewID = 0;
    view.sceneViewId = 0;
    EditorUtility.SetDirty(view);
}


Using versions
PUN 2.30
Unity 2019.4.13f1

Comments

  • Tobias
    Options
    Thanks for reporting this and also providing a fix. That's much appreciated.
    The change looks fine and I think I would add it.

    However, I really wonder how this happens?!


    PhotonNetwork.Instantiate and InstantiateRoomObject use NetworkInstantiate() and in there the view.sceneViewId is set to 0 for any instance (anything that is not loaded with the scene).

    Is photonViews = go.GetPhotonViewsInChildren() missing some PV on the object?
    Is the code missing in your case (due to failed import or ... whatever)?!
  • Sasq64
    Options

    Just to let you know, this happened to us and caused a lot of problems before I found this post.

    You really should add the fix.

    (But I noticed you have to open each prefab in turn to get it to write the correct sceneID again)