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;
}
}
}
}