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

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.

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

modernator
2021-08-03 06:50:24

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?

Comments

modernator
2021-08-03 07:17:04

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.

modernator
2021-08-03 07:21:57

In the play mode, VFXGenerator has:

  • View ID 3
  • IsMine True (master)
  • Controller [1] 'Local Player' (master)
  • Controller [2] 'Local Player' (master)
  • Creator [0]

The another manager script(called InGameUIManager)

  • View ID 0
  • IsMine False (master)
  • Controller [0]
  • Owner [0]
  • Creator [0]

Tobias
2021-08-03 08:55:36

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?

modernator
2021-08-03 09:57:55

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

Tobias
2021-08-03 13:23:02

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.

modernator
2021-08-04 07:57:17

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

Tobias
2021-08-04 08:40:24

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?

modernator
2021-08-04 10:28:11

@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().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);  
}  

modernator
2021-08-06 04:17:39

I'm still stuck in here and can't progress anything. Any help?

modernator
2021-08-07 07:25:19

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.

modernator
2021-08-07 11:57:09

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.

modernator
2021-08-10 19:07:55

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.

CallumHope
2021-11-16 15:26:23

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.

modernator
2021-11-16 17:08:45

CallumHope 2021-11-16T15:26:23+00:00

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.

AmIDuck
2022-02-20 23:01:38

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

AmIDuck
2022-02-20 23:02:46

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

modernator
2022-02-21 03:59:48

AmIDuck 2022-02-20T23:01:38+00:00

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

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
2022-07-27 22:27:04

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.

Tobias
2022-07-28 15:04:47

Lesnier 2022-07-27T22:27:04+00:00

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
2022-08-08 02:08:02

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.

stubing
2023-01-04 02:59:56

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

Screenshot 2023-01-03 at 6.58.32 PM.png

Uploaded 2023-01-04T02:59:44+00:00 902532 bytes

Back to top