Can't see players who have joined later

Options
Ok some peoples had posted this problem before but theirs solution looks a bit different to me.

I'm not sure exactly how to switch between scenes, so i'm probably doing this wrong but this is what i did:
- First we create a room and some players join on it. Once connected to the Room, on the room's Interface I'm not spawning real players yet, (only the gameObjectItem for listing theirs information such as name, team and lv using CustomProperties), when you hit StartGame, this is what is called:

RoomManager.cs
public void StartGame(){ //called when we hit StartGame Button
	PhotonNetwork.isMessageQueueRunning = false;
	PhotonNetwork.LoadLevel ("main");
}
Once we are in the scene the GameController script calls:
GameController.cs

void Start(){
	if (PhotonNetwork.inRoom) {
		// CALLED ONLY IF WE DID CAME FROM ANOTHER SCENE
		PhotonNetwork.isMessageQueueRunning = true;
		CreatePlayerObject();
	}

public void CreatePlayerObject(){
	GameObject newPlayerObject = PhotonNetwork.Instantiate( j.GetField ("chassis").str, mySpawnPosition.position, mySpawnPosition.rotation, 0);
// this is my player, now we configure some stuffs on him which is no important for us now...
}
In all cases my player always get instantiated normally but not the previously ones

Comments

  • Dannark
    Options
    Some Adicional Informations:
    Everything works just fine if I Enable this Teste Script and connect DIRECTLY in the map (main) scene where the players are spawned
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class NetworkManager : MonoBehaviour {
    
    	public string version = "War Drones v0.0.1";
    	public bool offlineMode = false;
    
    
    	void Start(){
    		//PhotonNetwork.autoJoinLobby = false; 
    		if (PhotonNetwork.inRoom) {
    
    		} else {
    			Connect ();
    		}
    	}
    
    	void Connect(){
    		if (offlineMode) {
    			PhotonNetwork.offlineMode = true;
    
    		} else {
    			PhotonNetwork.ConnectUsingSettings( version );
    		}
    
    	}
    
    	public virtual void OnConnectedToMaster(){
    		PhotonNetwork.JoinRandomRoom();
    	}
    
    	public virtual void OnJoinedLobby(){
    		//PhotonNetwork.GetRoomList()
    		PhotonNetwork.JoinRandomRoom();
    	}
    
    	public virtual void OnPhotonRandomJoinFailed(){
    		Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, " +
    			"so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
    		PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = 5 }, null);
    	}
    
    	public virtual void OnFailedToConnectToPhoton(DisconnectCause cause){
    		Debug.LogError("Cause: " + cause);
    		DebugConsole.Log ("OnFailedToConnectToPhoton: " + cause);
    	}
    }
    

    But in this case I also change the GameController.cs To this
    void Start(){
    	if (PhotonNetwork.inRoom) {
    		// CALLED ONLY IF WE DID CAME FROM ANOTHER SCENE
    		//PhotonNetwork.isMessageQueueRunning = true; // disabling it
    		//CreatePlayerObject();
    	}
    
    public void CreatePlayerObject(){
    	GameObject newPlayerObject = PhotonNetwork.Instantiate( j.GetField ("chassis").str, mySpawnPosition.position, mySpawnPosition.rotation, 0);
    // this is my player, now we configure some stuffs on him which is no important for us now...
    }
    
    void OnJoinedRoom(){ //creating player here instead
    	// CALLED ONLY IF WE DIDN'T CAME FROM ANOTHER SCENE
    	CreatePlayerObject();
    }
  • Dannark
    Dannark
    edited April 2017
    Options
    I just figured out what the REAL problem is...

    I've seen many peoples with the same problem as mine, but I was not able to fix this until now so... I Will try explain it

    First of all, the code was working just fine, only my logic was wrong. In this case You can't use PhotonNetwork.Instantiate() If the players connected to the same room are in different scenes (Actually you can but it Will breaks stuffs in this case) keep Reading...

    Just Think of it, lets say we have two scenes,
    <b>SceneA</b> and <b>SceneB</b>
    where SceneA have two players in a room ok? Now, one of the players loads the SceneB... once the very first player enter the SceneB you will probably call PhotonNetwork.Instantiate() what Will happens?

    It Will instantiate your prefab in ALL players that are currently connected to the same room! But there is a problem, there are others players still on SceneA, so... Yes! The player that have just entered on SceneB will be instantiated on SceneA (For the other players) instead of SceneB where him should really be!

    So when another player loads the SceneB, All wrong player that were inteantiated on SceneA(Wrongly) are going to be destroyed due the Scene loading process, and it will trow an warning message about the object id:xx being destroyed

    What I did to fix was to make a "ready button" so the master cliente will only starts the game(load the SceneB) If all others players are in a ready state, then i call a RPC to make all players load the Scene at the same time, together. So On the SceneB i've implemented some logic to wait until all players are actually on SceneB, so then, just Then we can finally and Safety call PhotonNetwork.Instantiate() because now ALL players are in the same Scene!

    Hope its helps!