I am making an Android multiplayer game using WiFi in Unity 5.
I open a hotspot on one Android device and connect the other Android device to it via WiFi. Then I start the game on both devices, host the game on one device and try to connect to the game through the other device, but I'm unable to connect to the game.
The following is my code:
public class GameMenu : MonoBehaviour
{
public GameObject PlayerPrefab;
void Start()
{
Application.runInBackground = true;
}
public void CreatePlayer()
{
connected = true;
GameObject g = Network.Instantiate(PlayerPrefab, transform.position, Quaternion.identity, 1) as GameObject;
GameObject.Find("PlayerCamera").GetComponent<Camera>().enabled = true;
Destroy(GameObject.Find("MenuCamera"));
}
void OnDisconnectedFromServer()
{
connected = false;
}
void OnPlayerDisconnected(NetworkPlayer pl)
{
Network.DestroyPlayerObjects(pl);
}
void OnConnectedToServer()
{
Debug.Log("Connected to Server");
CreatePlayer();
}
void OnServerInitialized()
{
Debug.Log("Server Initializied");
CreatePlayer();
}
bool connected;
void OnGUI()
{
if (!connected)
{
if (GUI.Button(new Rect(5, 70, 150, 70), "connect"))
{
Network.Connect("127.0.0.1", 25000);
}
GUI.enabled = true;
if (GUI.Button(new Rect(5, 150, 150, 70), "host"))
{
Network.InitializeServer(32, 25000, false);
}
GUI.Label(new Rect(5, 220, 200, 20), "Host IP Address: " + Network.player.ipAddress);
}
}
}