Leaving and Rejoining a room problem
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).
Leaving and Rejoining a room problem
LivingUniverse
2016-12-20 03:17:11
I also posted this on the Unity forums but i need an answer quick.
Just to let you know, I have a network manager with a photon view on it .
I'll explain what is happening visually:
Two people. 1 person on the unity editor, 1 person on a phone.
- Unity editor person hosts a match
- phone person joins and everything works fine, he leaves and then rejoins
- phone person on his side doesn't spawn
- unity editor person sees someone else spawn in the photon ID it says its not controlled locally but the name displayed came from the playerprefs of the unity editor person not the phone person, which means unity editor instantiated it.
-If phone person restarted the whole game and joins again it works fine. Spawns in normally like the very first instance of joining.
what is happening? Is having a photon view on the network manager and the player a problem?
This is what i do when i exit the room -
<pre class="CodeBlock"><code>public void ExitToMenu(){
PhotonNetwork.LeaveRoom();
GameObject networkManager = GameObject.Find("NetworkManager");
Destroy(networkManager);
}
void OnLeftRoom(){
PhotonNetwork.LoadLevel("Menu");
}</code></pre>
I Destroy the network manager so that there are no duplicates when i re enter the other scene which already has one. I have dontdestroyonLoad on it so i have to get rid of it first.
I can post my network manger script if you want to look at the code.
thanks
Comments
Hi @LivingUniverse,
I could not understand the issue you're describing. However, it seems like you're using NetworkManager as a persistent ("DontDestroyOnLoad") singleton in a wrong way. The idea of "DontDestroyOnLoad" is to instantiate the component once and never destroy it when a scene transition happens. So you probably need to stop destroying the NetworkManager and remove it from all scenes except the one who creates it. And in the NetworkManager creation logic always check if it's already created or not before trying to do so.
Probably you need to update your code accordingly. This is how I believe a persistent singleton across scenes should work.
LivingUniverse
2016-12-20 09:46:14
I use this code to check -
public static NetworkManager instance = null;
if(instance == null){
instance = this;
DontDestroyOnLoad(gameObject.transform);
}else if(instance != this){
Destroy(gameObject);
}
}
But it doesn't work when there is a photon view attached, it gives me this error - "PhotonView ID duplicate found: 14. New: View (0)14 on NetworkManager(Clone) (scene) old: View (0)14 on NetworkManager(Clone) (scene). Maybe one wasn't destroyed on scene load?! Check for 'DontDestroyOnLoad'. Destroying old entry, adding new"
It destroys both of them. So thats why i destroy it before i leave the other scene. What shall i do?
Please put the NetworkManager on a separate game object.
LivingUniverse
2016-12-20 19:49:59
separate gameobject to what?
@LivingUniverse please attach the NetworkManager to a separate GameObject that does not have a PhotonView attached to it and preferably a dedicated GameObject.
LivingUniverse
2016-12-21 20:28:22
but why does it work when i restart the game??
LivingUniverse
2016-12-22 09:45:41
i removed the photon ID and i didnt destroy the network manager when i left the scene but when i enter the room again it still doesnt work. It doesnt work when i rejoin a match, i still have to restart the whole game. I really need help, I have no idea why this is happening. Can i show you my code?
@LivingUniverse
What does not work?
Can you confirm that you have the NetworkManager on a separate GameObject?
The issue you described in the first post is still not clear to me.
I have few questions:
- Do you have any logic that relies on MasterClient?
- You mentioned that there was a player NickName mismatch and something about PlayerPrefs. How do you set the NickName?
From what I understood:
There is some logic being executed when the game starts or when the first game scene is loaded. Same logic is not executed on new game when the game scene is reloaded.
So you just have to look for where in the code you call it, debug it, easiest way is to throw some debug logs in there.
LivingUniverse
2016-12-22 19:26:52
ill tell you something that happens and then doesnt. When i first join the room my player spawns. If i restart the whole game and go back in the room my player spawns. If i exit the room with the button and go back in, my player doesnt spawn.
void start(){
isSpawned = false;
}
if(!isSpawned){
if(SceneManager.GetActiveScene().name == "Multiplayer"){
SpawnPlayer();
}
}
private void SpawnPlayer(){
for(int i = 0; i < playerPrefab.Length; i++){
if(PlayerPrefs.GetInt("SavedSelectedModel") == i){
GameObject insPlr = PhotonNetwork.Instantiate(playerPrefab[i].name, new Vector3(0, 0 , 0 ), Quaternion.identity, 0);
}
}
preparePlayers = true;
isSpawned = true;
}
This code runs in all cases, but my player doesnt spawn if i rejoin the match. I dont understand why. The other player in the game can see a player spawn from his side with the same name as him ( i cant see that player that spawned).
Please set isSpawned
to false
when you exit the game.
LivingUniverse
2016-12-22 21:16:32
it is false when i exit the game. I said the code runs, but my player doesnt spawn on my side. The other player sees a player spawn which i cant see and isnt in my hierarchy.
@LivingUniverse
First of all, I don't know why you need a for loop inside the SpawnPlayer method, I think you can do this instead and make sure Start method is spelled correctly (capital 'S'):
void Start(){
isSpawned = false;
}
private void SpawnPlayer(){
int savedSelectedModel = PlayerPrefs.GetInt("SavedSelectedModel");
if (playerPrefab != null && savedSelectedModel >= 0 && savedSelectedModel < playerPrefab.Length){
PhotonNetwork.Instantiate(playerPrefab[savedSelectedModel].name, new Vector3(0, 0 , 0 ), Quaternion.identity, 0);
preparePlayers = true;
isSpawned = true;
return;
}
Debug.LogErrorFormat("Could not spawn player, savedSelectedModel = {0}", savedSelectedModel);
}
I don't know when you save the "SavedSelectedModel" inside player prefs though.
And I don't know when or where this code is being executed, I guess it's inside the join callback of local player:
if(!isSpawned){
if(SceneManager.GetActiveScene().name == "Multiplayer"){
SpawnPlayer();
}
}
LivingUniverse
2016-12-23 16:48:50
i call that code in the network manager script in update, i tried it in On joined room but the same thing happens
@LivingUniverse
Can you merge this code with your NetworkManager then post the Unity console log here please.
LivingUniverse
2016-12-23 18:00:47
debug log said OnJoinedRoom - isSpawned = false, current scene multiplayer. SpawnPlayer selectedModel = 0;
http://paste.ofcode.org/GsATpVm7K9TYzt7g5GfWJZ
This is my network Manager code, i added the photon view back on to the game object because the problem was still there without it
LivingUniverse
2016-12-24 00:26:31
This doesnt just happen with spawn player. I have this other very simple script, originally it was attached to my networkmanager gameobject but i put it on its own gameobject but the same thing happens. http://paste.ofcode.org/buBigRvcLjmCxV7ut8VYG3 - This is the script to spawn some points into the arena. What happens, is it spawns them fine the first time or after i restart the unity game and then reenter the room. But when Player A exits the room and rejoins, they dont spawn for Player A and they arent in the hierarchy, but for Player B who is in the room too, they spawn for him and appear in his hierarchy.
Do you understand?
Edit - I added [PunRPC] and i think it works now. Im just testing it.
Ok so i think it works, everything seems to sync apart from when syncing the players points (int) when the player has 1 the other player sees 2. Im not sure why he can see 1 more.
but i also gets lots of these error messages http://pasteboard.co/1lGhg0GfA.png when i use PunRPC to spawn the player or the points. Do you know why?
@LivingUniverse
It looks like you're sending the PunRPC inside Update! Please try to remove as much logic as possible from Update.
LivingUniverse
2016-12-27 00:53:16
i put it in an on scene was loaded delegate and it runs the punrpc function once and it doesnt give me those messages so far, so thats good.
MushoodMunir
2018-05-02 15:34:42
Hi Do you able to fix this issue.
Currently i am facing this same issue and figuring out solution for it.
Leaving and Then Joining New Room Problem
I am stuck on this issue from past couple of days.
Just to let you know, I have a PlayerNetwork with a photon view on it.
I'll explain what is happening visually:
Two people. Both on the unity editor.
1 hosts a match
2nd person joins and everything works fine, Both leaves consecutively and then 1 create new room and other joins him
2nd person on his side doesn't spawn at all and also on his side there is no player instantaied
1st person sees single spawn on his side and that is his own. He doesn’t see other one instantaied
If one person restarted the whole game and joins again it works fine. Spawns in normally like the very first instance of joining.
I use this one for player instantaion
[PunRPC]
private void RPC_CreatePlayer ()
{
float randomValue = Random.Range (0f, 5f);
GameObject obj = PhotonNetwork.Instantiate (Path.Combine ("Prefabs", "NewPlayerZ"), Vector3.up * randomValue, Quaternion.identity, 0);
CurrentPlayer = obj.GetComponent ();
CurrentPlayer.photonView.enabled = true;
}
And i have this one attached to my Leave Game Button. And load lobby scene again from where player can create new room.
public void leaveMatch ()
{
PhotonNetwork.LeaveRoom ();
}
OnLeftRoom(){
PhotonNetwork.LoadLevel(0);
}
I think you're making your life harder than it needs to be by instantiating the object via RPC. The PhotonView needs to get a unique viewID, or it won't be able to be addressed by all players. This might be missing in your case.
Instead, use PhotonNetwork.Instantiate and PhotonNetwork.Destroy.
There is nothing in PUN that keeps you from running your scene's code once again, so you need to debug yourself, why it doesn't work. You could attach the SupportLogger component to a scene and keep it running. This will log creating, joining and leaving rooms. Make sure these events happen as intended and as working in the first run.
Back to top