I'm developing a racing game and am currently facing problems instantiating the race cars after loading the race-track scene.
I have a local "RaceGuard" that basically calls this method:
public void CreateLocalPlayer()
{
while (!PhotonNetwork.IsConnectedAndReady)
{
}
GameObject Player = PhotonNetwork.Instantiate(prefabName, Vector3.zero, Quaternion.identity);
PhotonView pV = Player.GetComponent<PhotonView>();
Player.transform.position = raceGuard.singleRaceStart1To7[pV.OwnerActorNr - 1].position;
Player.transform.rotation = raceGuard.singleRaceStart1To7[pV.OwnerActorNr - 1].rotation;
}
Now, I build the game and run it and run a second instance of the game inside Unity. When I test this, I usually encounter two scenarios:
1] Everything works as expected: Both games have both players at the correct position.
2] One game has only one car [the local player] while the other game has 3 cars in the scene [the local player, and then two remote players spawned at the same spot].
I struggle to find the cause of this issue, it's hard to debug aswell since I can only step through the code in the Unity instance, but nothing looks suspicious or throws an error. Including some prints, I can see that the "CarCreated" method gets called 3 times in the faulty case, but I don't know why.
Maybe I am doing something very basic wrong. Since it's my first time creating a multiplayer game, I watched tutorials, googled etc. but found nothing that describes my problem.
The first thing I would make sure is that you don't have more than 1 instance of your "RaceGuard" script in the scene. I don't think that's your issue though.
I think the most likely issue is that due to scene loading, the method you posted gets executed multiple times. Here's another person that had similar issues: https://forum.photonengine.com/discussion/13198/new-players-spawning-twice-on-existing-players
So make sure you don't load your scene multiple times.
Hope one of those suggestions solves your issue. Without having further info on your project setup this can be hard to debug.