Synch scene object over the network

Options
I've instantiated a scene gameobject over the network (not a player gameobject) and I want that every user in the room see that object. How to do it? Right now, even if it has a PhotonView component attached, only one player in the room can see it.
Code (in OnJoinedRoom function):
if (PhotonNetwork.isMasterClient) {
GameObject Timer = PhotonNetwork.InstantiateSceneObject("TimerInstance", Vector3.zero, Quaternion.identity, 0, null);
GameObject canvas = GameObject.FindWithTag("Canvas");
Timer.transform.parent = canvas.transform;
}
image

Comments

  • Kurtav
    Kurtav ✭✭
    Options
    interesting. Very often I see such errors in novices)) I had it too

    It is important to understand.
    Which line of code (network code) - is transmitted over the network.
    And what line (not the network code) - is not transmitted over the network.

    You have a gap in data transfer over the network

    Timer.transform.parent = canvas.transform; - this is not transmitted over the network
    Other players did not see that you changed the parent of the object.
    To change the parent it is possible for example in RPC functions. Then all the players will see this
  • StyleMaster
    Options
    Can you give me an example please?
  • StyleMaster
    Options
    I tried this, but without lucky:
    [PunRPC]
    void InstantiateTimer(string message)
    {
    Debug.Log("[RPC] " + message);
    timer = (GameObject)Instantiate(timerPrefab, transform.position + transform.forward, transform.rotation);
    timer.transform.parent = canvas.transform;
    }
    The gameobjct is created but not as parent of the Canvas. Some suggestions?
  • Kurtav
    Kurtav ✭✭
    edited May 2017
    Options

    The gameobjct is created but not as parent of the Canvas. Some suggestions?

    Two Unity opened? Checked in the scene the hierarchy of objects in both windows of the Unity?

    Do not properly create a network Instantiate object
    https://doc.photonengine.com/zh-cn/pun/current/manuals-and-demos/instantiation
    Last paragraph

    Or it is possible to take out an Instantiate from PunRPC and to create in OnJoinedRoom function and in rpc to change only the parent
  • StyleMaster
    Options
    I was meaning that for checking more users in the room I launch a standalone application and press Play in the Unity Editor (this way I can have two users in the same room).
    I've read the example, but I don't get how to set the parent. According to last paragraph, I should do something like this:

    public Transform spawnPosition; // drag and drop Canvas GO or Camera GO from Unity Editor
    void OnJoinedRoom() {
    // ...
    int id1 = PhotonNetwork.AllocateViewID();
    PhotonView photonView = this.GetComponent();
    photonView.RPC("SpawnOnNetwork", PhotonTargets.AllBuffered, Vector3.zero, Quaternion.identity, id1, timer);
    }
    [PunRPC]
    void SpawnOnNetwork(Vector3 pos, Quaternion rot, int id1, GameObject prefab)
    {
    Debug.Log("[PunRPC] SpawnOnNetwork");
    Transform newSceneGO = Instantiate(spawnPosition, pos, rot) as Transform;
    PhotonView[] nViews = newSceneGO.GetComponentsInChildren();
    nViews[0].viewID = id1;
    }

    What should I expect from this code? Nothing change.
    By the way is a scene gameobject not a player, so probably I should use InstantiateSceneObject
  • StyleMaster
    Options
    OK... so far, it seems to work this way, but probably is not the best solution.. is like a "do it anyway" solution :D Better solutions are welcome..
    When I press Space key, Checkmark and Timer prefabs are created. Checkmark become child of Canvas gameobject (parentPosition).

    Code:
    public class UploadPrefab : Photon.MonoBehaviour {
    public GameObject timer;
    GameObject timerPrefab;
    public GameObject checkmark;
    GameObject checkmarkPrefab;
    string timerPrefabName = "3DTimer";
    string checkmarkPrefabName = "checkmark1";
    public Transform parentPosition;
    Vector3 rescale = new Vector3(3, 3, 3);

    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Space))
    {
    timer = PhotonNetwork.InstantiateSceneObject(timerPrefabName, Vector3.zero, Quaternion.identity, 0, null);
    checkmark = PhotonNetwork.InstantiateSceneObject(checkmarkPrefabName, Vector3.zero, Quaternion.identity, 0, null);
    GameObject canvas = GameObject.FindWithTag("Canvas");
    checkmark.transform.parent = canvas.transform;
    checkmark.transform.localScale = rescale;
    timer.transform.position = new Vector3(0, 0, -1);
    int id1 = PhotonNetwork.AllocateViewID();
    PhotonView photonView = this.GetComponent();
    photonView.RPC("InstantiateTimer", PhotonTargets.AllBuffered, "Instantiate Timer!");
    }

    if (!PhotonNetwork.isMasterClient)
    {
    if (GameObject.Find("checkmark1(Clone)") != null)
    {
    var transform = parentPosition.transform;
    GameObject instantiated_checkmark = GameObject.Find("checkmark1(Clone)");
    instantiated_checkmark.transform.parent = transform;
    instantiated_checkmark.transform.localScale = rescale;

    GameObject instantiated_timer = GameObject.Find("3DTimer(Clone)");
    instantiated_timer.transform.position = new Vector3(0, 0, -1);
    }

    }

    }

    [PunRPC]
    public void InstantiateTimer(string message)
    {
    Debug.Log("[PunRPC] " + message);
    timer = (GameObject)Instantiate(Resources.Load("3DTimer", typeof(GameObject)) as GameObject);
    checkmark = (GameObject)Instantiate(Resources.Load("checkmark1", typeof(GameObject)) as GameObject);
    }
    }