- Home /
The question is answered, right answer was accepted
Photon Network instantiate problem when Master changes
Hi,
I'm using PhotonNetwork with Unity v2018.3.10f1.
I've created a "waiting room" to wait for the players to join a room before starting the game.
What I need is that when a player/s is waiting for others, if a player gets into the room, instantiate a prefab on every player in the room, and if he leaves the room, delete the prefab on every player.
First, it works perfectly, the player is instantiated on every player that is already in the room and is deleted when he leaves the room. But when is the Master Client who leaves the room, and rejoins it, is not working anymore and the player is not instantiated on any of the Photon players, nor on his scene.
How can I do to call the RPC method that instantiate the new player when the Master changes?
Code:
public void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
{
if (PV == null)
{
PV = GetComponent<PhotonView>();
}
if (PhotonNetwork.IsMasterClient)
{
Debug.Log("Creating lobbyPlayer");
PV.RPC("RPC_CreateLobbyPlayer", RpcTarget.AllBuffered);
}
}
[PunRPC]
private void RPC_CreateLobbyPlayer()
{
PhotonNetwork.Instantiate(lobbyPlayerPrefab, Vector3.zero, Quaternion.identity, 0);
}
Thanks!
why do you use a rpc to call a function that only uses photonnetwork instantiate?
Isn't the whole joke of PhotonNetwork.Instantiate
that it already creates a given object for all connected players? And as long as this object exists it will also be created for others? So your "buffered" rpc might actually do some things that are not beneficial to your cause.
Perhaps put in some more description on what you want to do.
Thanks, I've updated the info. I need the RPC buffered to instantiate the players object when other gets into the room, not only to the connected at the moment. It's my first time with PUN, maybe there is other way to do it?
I understand your problem but: As i said in my post: when you use Network instantiate on an object with a photon view, it will create said object for everyone else in the game. it will automatically be created for everyone else joining later on, as long as the owner of the object does not destroy it:
Please read the DOCU$$anonymous$$ENTATION on this
If your created objects do not have a photon view, please do not use PhotonNetwork.Instantiate for it. (As stated in the documentation)