New players spawning twice on existing players
The whole answer can be found below.
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).
New players spawning twice on existing players
Foxh9
2019-01-01 11:28:03
I have this problem when new players join the room, they get instantiated twice on all the existing players.
I read the code from the PunBasics demo and tried to replicate how it works but the problem still occurs no matter what I do.
I checked the logs on the new players and see that NetworkManager Start function gets called as many times as there are players and I believe this is normal Unity behavior but it should only spawn one player instance for each new player as there is a check to see if the player instance has been created already but it doesnt seem to work that way.
The NetworkManager is a scene object in every scene and it doesnt have a PhotonView component.
The playerPrefab gets instantiated by the NetworkManager in Start() function and that prefab has a PhotonView component.
After the playerPrefab gets instantiated, it instantiates another prefab on the owner's client that is a localPlayerPrefab which then receives input from the player and plays all animations etc.
Here is my code for the NetworkManager:
https://pastebin.com/RzrBM8eM
and here is code for the playerPrefab object that gets instantiated by the NetworkManager:
https://pastebin.com/WWUdVMav
I had pretty much the same setup when I was using the Unity network API and worked quite well but I cant get it to work with Photon. It might be a simple thing that I missed and just cant see.
Comments
So, generally speaking i instantiate players once. This would happen once the player joins a room and is NOT and RPC, simple a Photonnetwork instantiate in the start method of a game manager. That way each local instance is handling its own instantiation to the network. Doing it this way will prevent duplicates, etc.
Do you have example code for that? I thought my code was already doing what you describe but I see that when a new player joins the room, the NetworkManager object starts on them and the Start() function runs also for the existing players.
EDIT: I modified my code but now i cant test it in the editor because when i try to connect, it says "Room join failed, game doesnt exist" but i can join a room if i run two standalone builds....
I tested around and concluded that Start() method is called multiple times for new players when it shouldnt be.
I also tested in the PunBasics demo that comes with the Photon v2 package from AssetStore and there this behavior does not happen and Start() is called exactly once for all scene objects, as should be. WHY is that? What could be wrong with my Unity project?
EDIT: I found the solution. In "OnJoinedRoom" callback, I loaded a level even when the player is not a master client. So the level would be loaded twice for the new player and the NetworkManager Start() function was executed twice.
thanks that fixed it for me too
Yes Thanks! this was my problem too.
Electromaster20000
2022-02-17 03:09:16
THANK YOU SO MUCH!!! You do not know just how much this helped me out, I am currently writing the networking code of my Code Project game for my Computer Science A-level and this just helped me out so much with the double spawning issue. Again THANK YOU and I wish you the very best. PS: the solution was:
public override void OnJoinedRoom()
{
//feedBackText.text += "\nJoined Room with " + PhotonNetwork.CurrentRoom.PlayerCount + " players";
if (PhotonNetwork.IsMasterClient)
{
PhotonNetwork.LoadLevel("MyLevel");
}
}
Back to top