0

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);
        }
    }
}
Laurel
  • 5,965
  • 14
  • 31
  • 57
Madhusudan Sharma
  • 403
  • 1
  • 3
  • 12
  • 1
    `Network.Connect("127.0.0.1", 25000);`. One uses 127.0.0.1 if the server runs on the same device as the client. Thats not the case here. Your client should use the ip of the server. – greenapps Apr 24 '16 at 18:03
  • Thanks, that helped me and now I can connect to the server on other device. Thank you once again :) – Madhusudan Sharma Apr 30 '16 at 07:54

0 Answers0