- Home /
Photon.Instantiate being called twice in OnloadedLevel and/or OnroomJoin
Hey all I have been working with PUN and following the Sky Arena tutorials along with the examples. I've been scratching my head over this issue for two days with out much luck. What happening is say one client creates a room, second player connects to the room, both players get spawned but the second clients prefab is instantiated twice. The client can only move the second one spawned. The first just is suspended in the air and the second client cannot see its duplicate prefab. I hope that isnt too confusing, I have images and code as well. Thanks in advanced!
void OnJoinedRoom()
{
//Pause the message queue. While unity is loading a new level, updates from Photon are skipped.
//So we have to tell Photon to wait until we resume the queue again after the level is loaded. See MultiplayerConnector.OnLevelWasLoaded
PhotonNetwork.isMessageQueueRunning = false;
MapQueueEntry currentMap = MapQueue.GetCurrentMap();
Debug.Log( "OnJoinedRoom. Loading map: " + currentMap );
if( currentMap.Equals( MapQueueEntry.None ) )
{
PhotonNetwork.LeaveRoom();
return;
}
Application.LoadLevel( currentMap.Name );
if (Application.loadedLevelName == "Lobby" || Application.loadedLevelName == "Login") {
Debug.Log ("In Login or Lobby");
return;
}
else {
Debug.Log ("Spawn Player");
GameObject newPlayerObject = PhotonNetwork.Instantiate ("Roboot", Vector3.zero, Quaternion.identity, 0);
Debug.Log ("Set up Camera");
RPGCamera.GetCameraTarget();
//RPG_Camera.CameraSetup ();
}
}
Answer by SkaredCreations · Dec 14, 2014 at 11:51 AM
I think you're missing something, first of all you shouldn't call Application.LoadLevel because as soon as Unity loads the new level it stops the execution of your script (so neither of the next line after it are called). Instead you should call PhotonNetwork.LoadLevel on your master client and then call PhotonNetwork.Instantiate in the Awake method of your client in the level to be loaded. I suggest you to take a look at the demos attached to the PUN package, for example in DemoWorker and particularly at the scripts WorkerMenu (used in the main menu scene) and WorkerInGame (used in the game scene).
Answer by Sobe459 · Dec 15, 2014 at 03:40 AM
I looked over the Demos again, I've changed it to as close to it as I could. Still getting a phantom client owned by the second client to connect. I even added a PhotonView to the Gameobject holding the spawning script in the new scene like the worker demo. This is what I have came up with.
The callback manager script: void OnJoinedRoom() { //Pause the message queue. PhotonNetwork.isMessageQueueRunning = false;
PhotonNetwork.LoadLevel("TestMap1");
}
void OnLevelWasLoaded( int level )
{
Debug.Log( "OnLevelWasLoaded: " + Application.loadedLevelName );
//Resume the Photon message queue so we get all the updates.
PhotonNetwork.isMessageQueueRunning = true;
//Time is frozen at the end of a round, so make sure that we resume it when we load a new level
Time.timeScale = 1f;
}
The spawning script:
void Awake () {
if (Application.loadedLevelName == "Lobby" || Application.loadedLevelName == "Login")
{
//Debug.Log("In Login or Lobby");
return;
}
else
{
Debug.Log("Spawn Player");
GameObject newPlayerObject = PhotonNetwork.Instantiate("Roboot", Vector3.zero, Quaternion.identity, 0);
Debug.Log("Set up Camera");
RPGCamera.GetCameraTarget();
//RPG_Camera.CameraSetup ();
}
}
EDIT: After adding a bunch of debug longs, it seems to be that every client that joins get instantiated twice. The awake function in the spawning script is getting called twice on each client joining, i believe this is what is causing the "phantom" client that doesnt move.
2017 and I'm also having this problem, was there ever a solution found?
Hi,
can you share some more details? Or do you have exactly the same code as above?
I'm calling PhotonNetwork.Instantiate in the Start method after PhotonNetwork.LoadLevel.
If I start a server, connect a client, disconnect the server (client becomes master client) and rejoin, the phantom prefab appears.
Another thing I discovered today (which could be related) is when I connect to a server as a client the Start method on my prefab is called twice. I think the prefab is instantiated once, destroyed, and instantiated again. I know this because I was parenting a DontDestroyOnLoad object to my prefab but it got destroyed (presumably when the first instantiation was destroyed).
Hopefully this makes sense :)