Android multiplayer using WiFi

Hello everyone, I want to develop an android game which go multiplayer over the wifi (something like “Doodle Army 2: Mini Militia”). I have tried everything which is available on the internet but nothing helped me. I made a game which can go multiplayer on the same system(computer) but when I try it on android devices(making one android device as hotspot and other to connect to it via wifi), it doesn’t works. I don’t know what is the problem. I am using the following script to handle all of the networking related tasks. Please help! Thank you.

using UnityEngine;
using UnityEngine.Networking;
using gui = UnityEngine.GUILayout;

public class Network : MonoBehaviour
{
public GameObject PlayerPrefab;
string ip = “127.0.0.1”;
private const string typeName = “typename”;
private const string gameName = “gamename”;
private HostData hostList;

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();
}
private void RefreshHostList()
{
    MasterServer.RequestHostList(typeName);
}
void OnMasterServerEvent(MasterServerEvent msEvent)
{
    if (msEvent == MasterServerEvent.HostListReceived)
        hostList = MasterServer.PollHostList();
}
bool connected;
void OnGUI()
{
    if (!connected)
    {
        ip = GUI.TextField(new Rect(5, 5, 150, 50), ip, 25);
        if (GUI.Button(new Rect(5, 70, 150, 70), "connect"))
        {
            Network.Connect(hostList[0]);      //Both ways are tried but
            Network.Connect("127.0.0.1", 25000, "");     //nothing works.
        }
        GUI.enabled = true;
        if (GUI.Button(new Rect(5, 150, 150, 70), "host"))
        {
            Network.InitializeServer(32, 25000, false);
            MasterServer.RegisterHost(typeName, gameName);
            Network.sendRate = 15;
        }
    }
}

}

@madhusudan1997-sharma

You should use 192.168.43.1 for ip.
It worked for me on a multiplayer hotspot game.

you’re trying to make the android device which is the client connect to itself. You need to replace the “127.0.0.1” with the ip address of the host device. I think you can find this somewhere in the wifi settings, but it should be the same for all android devices who are running mobile hotspot.

Sorry if you’ve tried this already.

@magnas94 I just build a project in Unity 5.3 which runs over local multiplayer, (using hotspot), So I have solution to your problem. I see you are working on uNet. I have created my game in uNet too… So do let me know if you still need help . :slight_smile:

I also wanted to make the same game, can you help me?

@RiAmazing , Its good that you want to work on a similar type of game , so are you using uNet too ? and if you are , then will you be connecting your devices using WiFi, or mobile Hotspot ??