Issues When Adding PhotonViews to GameObjects at Runtime

Hello!

I am working on a game where I need to be able to add and remove PhotonViews to objects at runtime relatively frequently. I have a system where I add a PhotonView on the client side, then send the name of the GameObject, and the ViewID that I allocated to the PhotonView through a custom PhotonView (lets just call it SharedInfo) script that I have created. SharedInfo then sends the data over the network to itself (serializes it? I'm not sure what the technical term is for this). It uses the name that is sent over the network to find the corrosponding GameObject and creates a PhotonView on that object, with a ViewID that is the same as the one sent over the network. Sorry if I explained this poorly.

Anyways, when the master client runs this, everything works fine. But the weird thing is that when a non-master client runs this, I get the following warning.

Received OnSerialization for view ID XXXX. We have no such PhotonView! Ignored this if you're leaving a room. State: Joined

What is weird is that the warning doesn't appear at all on the non-master clients. It only appears on the master client when a non-master client tries to run through this "process". What I also find strange is that it seems to be stopping SharedInfo from reading anything from the stream on the master client (as I found out by putting a debug.log in the start of if (stream.isreading), and seeing that the debug.log never showed up in the console).

Again, I'm sorry if this is explained poorly. But does anybody have any ideas as to why this might be happening?

Comments

  • After a bit more testing, I discovered that the warning does appear on non-master clients if another non-master client is what runs the process. Furthermore, if i don't ever add anything into the observed components of the PhotonView that I create at runtime, the warning goes away. In other words, the warnings only appear when there is something in the observed components.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @Stekon,

    Sorry for the long delay!

    In order to be synchronized over the network, PUN requires GameObject with PhotonView components to be created using one of the following 4 methods:

    - PhotonNetwork.Instantiate
    - PhotonNetwork.InstantiateSceneObject
    - GameObject with PhotonView already in the scene
    - Manual instantiation

    If you want to sync. a GameObject with PhotonView dynamically added at runtime, it's not recommended and could be a lot of trouble.
    Unless the use case really requires this, use one of the methods supported out of the box.
  • Hi,

    The code which is in the documentation about the manual instantiation doesnt work.

    The "OnEvent" is never called, the object on the client is never instantiated.

    Can you please help me out?

    Here is my code:

    using Photon.Pun;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using ExitGames.Client.Photon;
    using Photon.Realtime;

    public class PhotonManualInstantiate : MonoBehaviour
    {
    public GameObject PlayerPrefab;

    public byte CustomManualInstantiationEventCode;

    [ContextMenu("SpawnPlayer")]
    public void SpawnPlayer()
    {
    GameObject player = Instantiate(PlayerPrefab);
    PhotonView photonView = player.GetComponent<PhotonView>();

    if (PhotonNetwork.AllocateViewID(photonView))
    {
    object[] data = new object[]
    {
    player.transform.position, player.transform.rotation, photonView.ViewID
    };

    RaiseEventOptions raiseEventOptions = new RaiseEventOptions
    {
    Receivers = ReceiverGroup.Others,
    CachingOption = EventCaching.AddToRoomCache
    };

    SendOptions sendOptions = new SendOptions
    {
    Reliability = true
    };

    PhotonNetwork.RaiseEvent(CustomManualInstantiationEventCode, data, raiseEventOptions, sendOptions);
    }
    else
    {
    Debug.LogError("Failed to allocate a ViewId.");

    Destroy(player);
    }
    }

    public void OnEvent(EventData photonEvent)
    {
    if (photonEvent.Code == CustomManualInstantiationEventCode)
    {
    object[] data = (object[])photonEvent.CustomData;

    GameObject player = (GameObject)Instantiate(PlayerPrefab, (Vector3)data[0], (Quaternion)data[1]);
    PhotonView photonView = player.GetComponent<PhotonView>();
    photonView.ViewID = (int)data[2];
    }
    }
    }
  • Hi @DirtyFred, you missed reading the section where it mentions to register the OnEvent callback handler. Refer the RPCs and RaiseEvent section - https://doc.photonengine.com/en-us/pun/v2/gameplay/RpcsAndRaiseEvent#raiseevent