Start game once 2 players join Photon

I have decided to try out photon but i am having issues when i try to hold player1 while another player joins to start the game.

Player1 connects to the room and i display waiting for players if the player count is ==1, That works fine. I then connect Player2 and that meets the condition of count is ==2 and enters the next scene where i instatiate my prefabs.

The problem is Player1 just hangs in the waiting for players scene. How can i tell Player1 the count is now ==2 and join the game?

This is my code for scene 1

{
    private PhotonView myPhotonView;

	//public SpawnSpot[] spawnspots;

	public GameObject popup;
	
		
		// Use this for initialization
    void Start()
    {
        PhotonNetwork.ConnectUsingSettings("0.1");
		//spawnspots = GameObject.FindObjectsOfType<SpawnSpot>();
    }

    public override void OnJoinedLobby()
    {
        Debug.Log("JoinRandom");
        PhotonNetwork.JoinRandomRoom();
    }

    public void OnPhotonRandomJoinFailed()
    {
        PhotonNetwork.CreateRoom(null);
    }

    public override void OnJoinedRoom()
    {	
		if (PhotonNetwork.playerList.Length == 2) {
		    PhotonNetwork.LoadLevel ("StandardTrack");
		} 
		else if (PhotonNetwork.playerList.Length == 1) {
			Debug.Log ("Not Enough PLayers");
			popup.SetActive (true);
		}
	}
	
    public void OnGUI()
    {
        GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());

        if (PhotonNetwork.connectionStateDetailed == PeerState.Joined)
        {
            bool shoutMarco = GameLogic.playerWhoIsIt == PhotonNetwork.player.ID;

            if (shoutMarco && GUILayout.Button("Marco!"))
            {
                myPhotonView.RPC("Marco", PhotonTargets.All);
            }
            if (!shoutMarco && GUILayout.Button("Polo!"))
            {
                myPhotonView.RPC("Polo", PhotonTargets.All);
            }
        }
    }
}

This is the code for scene 2

public class LoadPlayers : Photon.PunBehaviour {

	private PhotonView myPhotonView;
	
	public SpawnSpot[] spawnspots;


	public void Awake(){

		spawnspots = GameObject.FindObjectsOfType<SpawnSpot>();

	}

	public void Start(){

		SpawnPlayers ();

	}


	public void SpawnPlayers(){

		if (PhotonNetwork.playerList.Length == 1) {
			SpawnSpot mySpawnSpot = spawnspots [Random.Range (0, spawnspots.Length)];
			GameObject monster = PhotonNetwork.Instantiate ("Player1", mySpawnSpot.transform.position, Quaternion.identity, 0);
			monster.GetComponent<myThirdPersonController> ().isControllable = true;
			myPhotonView = monster.GetComponent<PhotonView> ();
			
		}
		
		if (PhotonNetwork.playerList.Length == 2) {
			SpawnSpot mySpawnSpot = spawnspots[Random.Range (0, spawnspots.Length) ];
			GameObject monster = PhotonNetwork.Instantiate ("Player2", mySpawnSpot.transform.position, Quaternion.identity, 0);
			monster.GetComponent<myThirdPersonController> ().isControllable = true;
			myPhotonView = monster.GetComponent<PhotonView> ();
		}

	}

}

You need to add a call for when the other player joins, you have two options that I can think of. One is to just add:

void OnPhotonPlayerConnected(PhotonPlayer newPlayer){
     //Doesn't get called on the local player, just remote players, so you would still need something to handle on the second player
    if (PhotonNetwork.playerList.Length == 2) {
         PhotonNetwork.LoadLevel ("StandardTrack");
     } 
     else if (PhotonNetwork.playerList.Length == 1) {
         Debug.Log ("Not Enough PLayers");
         popup.SetActive (true);
     }
 }

That way it gets called when another player joins.

The other way would be to write an RPC that gets called when player 2 joins to update player 1, that one’s probably less desirable since there’s a built in call that accomplishes what you need.

In one of our project’s we tend to use more authoritative calls for things like this, we have the “host” player send out an RPC to all players to switch scenes, so that there are not multiple calls going on and everyone gets the call at relatively the same time.