Some of the scene object's view id changes to 0 when I run the game

I have a bunch of "manager" scripts, and the manager objects are saved as prefab and used in multiple scenes, over 20+. They are already in the scenes, not instantiated dynamically.

Some of the managers need to sync the values, so I put the PhotonView component, but they always have a view id of 0, and calling RPC always fails.

After some research, there is something like "PunSceneSettingsFile", to set minimum view id(have no idea what that means), and tried assigning my scene, but doesn't make any difference.

The weirdest thing is that there is another manager called "VFXGenerator" to synchronize VFX effects. This object has a view ID so that I can call RPC without any problems. I didn't use PhotonNetwork.Instantiate and there's no extra code for assigning view id manually. There's no difference between VFXGenerator and all the other manager scripts, but why only do the VFXGenerator works and others not?

Answers

  • I just checked the scene and saw that the manager has a view id of 1. But when I run the game, IDK why but it becomes zero. VFXGenerator had a view id of 3, and run the game, still has 3.
  • In the play mode, VFXGenerator has:
    - View ID 3
    - IsMine True (master)
    - Controller [1] 'Local Player' (master)
    - Controller [2] 'Local Player' (master)
    - Creator [0] <null>

    The another manager script(called InGameUIManager)
    - View ID 0
    - IsMine False (master)
    - Controller [0] <null>
    - Owner [0] <null>
    - Creator [0] <null>
  • A viewID of 0 is not "legal" for a PhotonView at runtime but it's a value we use in prefabs.

    PhotonViews in scenes should have some viewID > 0 and < 1000 assigned. The PunSceneSettingsFile assigns the minimal viewID to use per scene, so that PhotonView.ViewIDs from different scenes don't overlap.

    Please update to the latest PUN 2 version (2.34.1) and check if this reproduces the issue.
    Do you ever assign any ViewID values?
  • @Tobias Thank you for the response.

    No, I never assign any ViewID manually, I just leave everything default. When I opened my scene from Unity editor, the prefab objects have PhotonView has a ViewID like 1 or 3. But when I go play or build the game and run, View ID suddenly became zero. There is no specific PUN-related code in Awake and Start at all.

    I forgot to tell you, but I'm already using the latest version of PUN 2.
  • Make sure that the same ViewID is not used in multiple scenes. That should be all.
    If this doesn't help, I'd add some Debug.Log to the ViewID field and log any set call for the property, which applies 0. There should be some hint of a problem in the logs already, however.
  • Tobias wrote: »
    Make sure that the same ViewID is not used in multiple scenes. That should be all.

    I just checked every PhotonView's ViewID but there's no same View ID used. The View ID of 1 is always used in InGameManager. Note that this value is assigned from Photon, not by me. I already mentioned but everything left default.

    Tobias wrote: »
    If this doesn't help, I'd add some Debug.Log to the ViewID field and log any set call for the property, which applies 0. There should be some hint of a problem in the logs already, however.

    There are no such features to change View ID in my code so there's no place to put the Debug.Log. Log the ViewID of InGameManager always has zero. Also, I set Pun Logging to "Full" and enabled Support Logger, but there are no related logs found in the console. Only I can get is:
    Registered PhotonView: 1
    UnityEngine.Debug:Log(Object)
    Photon.Pun.PhotonNetwork:RegisterPhotonView(PhotonView) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1023)
    Photon.Pun.PhotonView:set_ViewID(Int32) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:314)
    Photon.Pun.PhotonView:Awake() (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:341)
    

    But there is no log that sets ViewID 1 to 0, but InGameManager has 0.
  • Maybe you Instantiate the object without using PhotonNetwork.Instantiate() at some point?
    Are you loading the scene multiple times or is it loaded at the beginning?

    Remove the component on the prefab and set it up again. If you linked some other component to that PhotonView, Unity may run into a quirk where it references the prefab's "instance", not the instance in the scene's object. Try to figure out if that's the case.
    Or do you find the PV?
  • Tobias wrote: »
    Maybe you Instantiate the object without using PhotonNetwork.Instantiate() at some point?

    Nope, InGameManager is already in the scene, not instantiated dynamically.
    Tobias wrote: »
    Are you loading the scene multiple times or is it loaded at the beginning?

    There are total of two scene loads, "Boot" -> "Start" -> "Game", but only "Game" scene contains InGameManager. Boot and Start scene even don't have any PhotonVIews.
    Tobias wrote: »
    Remove the component on the prefab and set it up again.

    Already tried several times, but no luck.
    Tobias wrote: »
    Unity may run into a quirk where it references the prefab's "instance", not the instance in the scene's object. Try to figure out if that's the case.

    I'm sorry, but I'm not sure what you want me to check. The only I can say is that I can confirm that InGameManager has a view id of 1 in the edit mode, and suddenly became 0 after loading the scene.
    Tobias wrote: »
    Or do you find the PV?
    Find from where? GetComponent<PhotonView>().ViewID also returns zero.

    However, I just discovered that if I run the "Game" scene directly, which contains InGameManager, it has the correct ViewID.

    So, it seems like loading a scene is the matter? But there is no PhotonView used in the previous scenes.

    What can cause change the ViewID of InGameManager to zero in this case? Funny thing is that there are more manager scripts that have PhotonView works without any problems. Only InGameManager has the problem.

    And one more thing, this may be related, but I'm not using PhotonNetwork.AutomaticallySyncScene to synchronize the scene, instead of using custom events.
    public static void LoadScene(string sceneName) {
        object[] content = new object[] {
            sceneName,
        };
    
        RaiseEventOptions raiseEventOptions = new RaiseEventOptions() {
            Receivers = ReceiverGroup.All,
        };
    
        PhotonNetwork.RaiseEvent(MultiplayerEventCode.LoadScene, content, raiseEventOptions, SendOptions.SendReliable);
    }
    
    ...
    
    public void OnEvent(EventData photonEvent) {
        byte eventCode = photonEvent.Code;
    
        if (eventCode == MultiplayerEventCode.LoadScene) {
            HandleLoadScene(photonEvent);
        }
    }
    
    void HandleLoadScene(EventData photonEvent) {
        object[] data = (object[]) photonEvent.CustomData;
    
        data = (object[]) photonEvent.CustomData;
    
        string sceneName = (string) data[0];
        SceneLoader.LoadScene(sceneName);
    }
    
  • I'm still stuck in here and can't progress anything. Any help?
  • modernator
    edited August 2021
    I added debug.log in PhotonView.cs line 337:
    /// <summary>Will FindObservables() and assign the sceneViewId, if that is != 0. This initializes the PhotonView if loaded with the scene. Called once by Unity, when this instance is created.</summary>
    protected internal void Awake()
    {
        if (this.ViewID != 0)
        {
            return;
        }
    
        Debug.Log(transform.name + " / " + this.sceneViewId);
    
        if (this.sceneViewId != 0)
        {
            // PhotonNetwork.Instantiate will set a ViewID != 0 before the object awakes. So only objects loaded with the scene ever use the sceneViewId (even if the obj got pooled)
            this.ViewID = this.sceneViewId;
        }
    
        this.FindObservables();
    }
    

    I get these:
    - InGameManager / 0
    - VFXGenerator / 3
    - GameMode / 1

    https://drive.google.com/file/d/1xZQ_z3_A4nh1MKrL2mWg0Q2mfOyAMAeH/view?usp=sharing

    From the scene, I can clearly see that InGameManager assigned a View ID of 2. VFXGenerator and GameMode work fine, only InGameManager loses the scene view id after loading the scene.
  • Still have no idea why this happens, and I just gave up.

    Temporarily fixed by obtaining the view id from the master, and used RaiseEvent to force sync the view id of very specific objects.
  • This is still kept happening to me, and if I edit anything in the prefab with contains PhotonView, the problem goes to another PhotoView randomly. So I removed all PhotonViews in the prefab and used RaiseEvent to create the photon views dynamically. I'm not sure why this is happening, but one thing I can guess is that the prefab I'm using has multiple photon views in the multiple children. Maybe nested photon view causes this behavior? Have no idea.
  • I am also having this issue.

    Objects that are saved in the scene with a photon view manually set to one that we are positive are not used anywhere else are being assigned the id of 0 on scene load.

    This appears to happen on a random bases about 70% of the time, sometimes it does indeed keep the assigned IDs, but most of the time it is reassigned 0.


  • This issue still happens to me these days. Every object in the scene with PhotonView causes this trouble, so I had to replace them all to create dynamically using PhotonNetwork.Instantiate, or use the Custom event to manually assign the view ID forcibly.

    I thought that it was related to nested PhotonView, but it wasn't. It just won't work. Opening the scene in the Unity Editor(not playing mode), they have a ViewID none of zero, but once the scene is loaded in play mode, it always always always have 0.

  • I am also having this issue. Did u find something?

  • Which are placed on the stage from the very beginning, without Instantiate

  • Unfortunately, not. I had to replace whole scene objects to be generated dynamically by PhotonNetwork.Instantiate. It sucks, but at least works for now.

  • Lesnier
    Lesnier
    edited July 2022

    I have the same problem once the scene is loaded by photonloadlevel with AutomaticallySyncScene the id changes to 0 in the clients master client keeps 1.

  • Please create a new thread and explain the problem with some context. This thread is ages old and we don't even know which version of PUN you use.

    Thanks.

  • Lesnier
    Lesnier
    edited August 2022

    I think i found the problem it only happens when i modify any prefab with photonview and scene is not saved or the changes made to prefab are not overwriten before playing.

  • I was running into this exact problem. How I fixed the issue was changing TransferOwnership in my photon views from "Fixed" to "TakeOver"