Reputation: 55
I have written my Photon script so that the player joins a random room, and if no room is found, the player will automatically create a new room. However, when I build and run my game on two different computers, no room is found on both of them, so they both create their own room. Please can someone tell me why?
The game starts like it should when there only 1 player is required, but when 2 are required it doesn't, because of the issue I mentioned above.
using UnityEngine;
using System.Collections;
public class NetworkManager : Photon.PunBehaviour
{
public string playerprefabname = "player";
Vector3 spawner = new Vector3(9.9f, -3.8f, -0.1f);
// Use this for initialization
void Start()
{
//Log stuff to console
PhotonNetwork.logLevel = PhotonLogLevel.Full;
//Connect
PhotonNetwork.ConnectUsingSettings("v0.1");
//Sync scenes
PhotonNetwork.automaticallySyncScene = true;
}
//Display connection state on game
void OnGUI()
{
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
PhotonNetwork.JoinRandomRoom();
}
//Create a room if fail to join one
void OnPhotonRandomJoinFailed()
{
Debug.Log("Can't join random room!");
RoomOptions roomOptions = new RoomOptions() { isVisible = false, maxPlayers = 2 };
PhotonNetwork.CreateRoom(null, roomOptions, TypedLobby.Default);
}
// when joined to a room check if 3 players are there, then send to level
public override void OnJoinedRoom()
{
if (PhotonNetwork.playerList.Length == 2)
{
Debug.Log("2 Players In Room Starting Level");
GameObject myPlayer = PhotonNetwork.Instantiate(playerprefabname, spawner, spawnpoint.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 = myPlayer;
}
}
}
}
public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
{
if (PhotonNetwork.playerList.Length == 2)
{
Debug.Log("2 Players In Room Starting Level");
GameObject myPlayer = PhotonNetwork.Instantiate(playerprefabname, spawner, spawnpoint.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 = myPlayer;
}
}
}
}
}
Upvotes: 0
Views: 2596
Reputation: 709
The reason is isVisible option set to false when you create the room. Because of that room is not shown in room list and can not be chosen by random join.
Upvotes: 2
Reputation: 1
were the computers connected to each other like via LAN or wireless-ly? it creates a room when there is no connection between the two computers.
Upvotes: 0