Sometimes ConnectAndJoinRandom script works, sometimes doesn't

Options
I am using the ConnectAndJoinRandom (modified slightly by myself), and usually it works but sometimes once the first player makes the room, other players will not connect to that room and make their own room instead. Why is this?






using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Linq;


public class ConnectAndJoinRandom : Photon.MonoBehaviour
{

public GameObject myPlayer;
public GameObject secondPlayer;

public int number;

public int count;


private GameObject spawnpoint;



public string playerprefabname = "player";

Vector3 spawner = new Vector3(9.9f, -3.8f, -0.1f);

Text loadtext;



/// Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts. public bool AutoConnect = true;

public byte Version = 1;

/// if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings() private bool ConnectInUpdate = true;

public virtual void Start()
{

PlayerPrefs.SetInt ("ready", 0);
count = 0;

PhotonNetwork.autoJoinLobby = false; // we join randomly. always. no need to join a lobby to get the list of rooms.


}

public virtual void Update()
{




loadtext = GameObject.Find("loadtext").GetComponent();

if (PhotonNetwork.playerList.Length == 1)
{
loadtext.text = "1/3";

}
if (PhotonNetwork.playerList.Length == 2)
{

loadtext.text = "2/3";

}
if (PhotonNetwork.playerList.Length == 3)
{

PhotonNetwork.room.open = false;
loadtext.text = "3/3";

}
if (ConnectInUpdate && AutoConnect && !PhotonNetwork.connected)
{
Debug.Log("Update() was called by Unity. Scene is loaded. Let's connect to the Photon Master Server. Calling: PhotonNetwork.ConnectUsingSettings();");

ConnectInUpdate = false;
PhotonNetwork.ConnectUsingSettings(Version + "." + Application.loadedLevel);
}
}

// to react to events "connected" and (expected) error "failed to join random room", we implement some methods. PhotonNetworkingMessage lists all available methods!

public virtual void OnConnectedToMaster()
{
if (PhotonNetwork.networkingPeer.AvailableRegions != null) Debug.LogWarning("List of available regions counts " + PhotonNetwork.networkingPeer.AvailableRegions.Count + ". First: " + PhotonNetwork.networkingPeer.AvailableRegions[0] + " \t Current Region: " + PhotonNetwork.networkingPeer.CloudRegion);
Debug.Log("OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");
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 = 3 }, null);
}

// the following methods are implemented to give you some context. re-implement them as needed.

public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)
{
Debug.LogError("Cause: " + cause);
}








public void OnJoinedRoom()
{

++count;
if (PhotonNetwork.playerList.Length == 3 )
{

loadtext.transform.localScale = new Vector3(0f, 0f, 0f);
Debug.Log("2 Players In Room Starting Level");
GameObject.Find("findinggame").transform.localScale = new Vector3(0, 0, 0);

if (count == 1)
{
spawnpoint = GameObject.Find("spawnpoint");
}
}

spawnpoint.transform.localEulerAngles = new Vector3(348f, 357f, 12f);

GameObject myPlayer = PhotonNetwork.Instantiate(playerprefabname, spawnpoint.transform.position, spawnpoint.transform.rotation, 0);
myPlayer.name = "player11";



//GameObject MyCam = PhotonNetwork.Instantiate ("Camera", CamPos, Quaternion.identity, 0);
GameObject camera = GameObject.FindWithTag("MainCamera");

if (camera != null)
{
CameraController followScript = camera.GetComponent("CameraController") as CameraController;
if (followScript != null)
{
followScript.target = myPlayer;
}
}



}

public void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
{

++count;

if (PhotonNetwork.playerList.Length == 3)
{

loadtext.transform.localScale = new Vector3(0f, 0f, 0f);
GameObject.Find("findinggame").transform.localScale = new Vector3(0, 0, 0);


Debug.Log("2 Players In Room Starting Level");


if (count == 2)
{

spawnpoint = GameObject.Find("spawnpoint2");



}

if (count == 3)
{

spawnpoint = GameObject.Find("spawnpoint3");



}



}

spawnpoint.transform.localEulerAngles = new Vector3(348f, 357f, 12f);

GameObject secondPlayer = PhotonNetwork.Instantiate(playerprefabname, spawnpoint.transform.position, spawnpoint.transform.rotation, 0);



//GameObject MyCam = PhotonNetwork.Instantiate ("Camera", CamPos, Quaternion.identity, 0);
GameObject camera = GameObject.FindWithTag("MainCamera");

if (camera != null)
{
CameraController followScript = camera.GetComponent("CameraController") as CameraController;
if (followScript != null)
{
followScript.target = secondPlayer;
}
}
}

void OnPhotonPlayerDisconnected()
{

--count;

}
}



Thanks

Comments

  • vadim
    Options
    Does unmodified script version work for you (in your project or in PUN demo)?
    What is in logs when join fails?