Photon Instatiate problem

Options
Hello,

I have a massive issue. I want objects to be spawned when the server is created and everyone who connects can see the object.

As i only want it to spawn the object when the room is first created and not joined, i added this:
void OnCreatedRoom()
    {
    PhotonNetwork.Instantiate ("tree", tree1.transform.position, tree1.transform.rotation, 0);
}


This works fine. When the room is created the tree is spawned in the correct place. The issue is, say another person connected, it would create another tree..

So two players = two trees but i just want it so there is one tree for all players. I could simply just make it client side however it needs to be on network for reasons.

If anyone could tell me why its instantiating a tree per person i would be greatful.

Comments

  • vadim
    Options
    For what object OnCreatedRoom() is implemented? Are you sure that OnCreatedRoom called several times during one room session? Can you prove that with logs?

    OnCreatedRoom should be called once and only on one client. Moreover, it's possible that it will never get called during room lifetime, as noticed in docs:
    /// As any client might close (or drop connection) anytime, there is a chance that the
    /// creator of a room does not execute OnCreatedRoom.
    ///
    /// If you need specific room properties or a "start signal", it is safer to implement
    /// OnMasterClientSwitched() and to make the new MasterClient check the room's state.

    So you need to implement OnMasterClientSwitched also and check there if scene already initialized.

    For spawning objects not owned by anyone like trees use PhotonNetwork.InstantiateSceneObject call on master client. That ensures that tree persists after creator left the room.