Ok, So I’m creating a multiplayer “board game” for Android and Im trying to override or virtual the OnServerAddPlayer() function so that a custom player prefab is instatiated. So, I don’t have a player prefab set in the Network Manager through the inspector. I just figured I could register the prefab through code as you can see below. I turned off “AutoAddPLayer” because Unity was throwing an error asking for a player prefab. So now the prefab wont spawn but I can see that OnServerAddplayer is being called. i have a few buttons I’ve made that disable/enable other buttons to allow the player to go through the UI menu. They first see a join game and host game button. host game calls OnPressHostBtn () and Join game calls OnPressJoinGameBtn() which enables an input element that allows you to enter an IP address then the next Join button calls OnPressJoin() which shows the three race buttons the player can select from. then when the player presses OnPressTerranIP() the IP from the IP window is loaded into the NetworkManager.singleton.networkaddress and the singleton client is started.
The main issue for me is that the unity documentation doesn’t detail what functions need to be in the overridden functions like OnServerConnect() so I don’t know if there is something missing there. I’ve been struggling with this for hours and am stumped at this point. any help would be great thanks!
note: if I comment out the OnServerConnect() and enable AutoAddPlayer the local Client is able to connect to the Server via the loopback address and spawns the prefab. but the remote client will not spawn the player prefab
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class MainCanvasCtr : NetworkManager {
public GameObject UTCBtnOBJ;
public GameObject KamBtnOBJ;
public GameObject XydBtnOBJ;
public GameObject UTCBtnIPOBJ;
public GameObject KamBtnIPOBJ;
public GameObject XydBtnIPOBJ;
public GameObject IPDispOBJ;
public GameObject Titleimg;
public GameObject TitleSkyImg;
public GameObject JoinGameBtn;
public GameObject HostGameBtn;
int SelectedRace = 0;
public GameObject UTCPreFab;
public GameObject KamPreFab;
public GameObject XydPreFab;
public GameObject TilePreFab;
public GameObject UnitPreFab;
public GameObject JoinBtn;
public GameObject IpInput;
public GameObject EnterIPMsg;
public NetworkClient myClient;
void Start ()
{
HostGameBtn.SetActive(true);
JoinGameBtn.SetActive(true);
UTCBtnOBJ.SetActive(false);
KamBtnOBJ.SetActive(false);
XydBtnOBJ.SetActive(false);
UTCBtnIPOBJ.SetActive(false);
KamBtnIPOBJ.SetActive(false);
XydBtnIPOBJ.SetActive(false);
IpInput.SetActive(false);
EnterIPMsg.SetActive(false);
JoinBtn.SetActive(false);
NetworkManager.singleton.networkPort = 7777;
}
public void OnPressHostBtn ()
{
NetworkManager.singleton.maxConnections = 3;
NetworkManager.singleton.StartServer();
ClientScene.RegisterPrefab(TilePreFab);
ClientScene.RegisterPrefab(UnitPreFab);
ClientScene.RegisterPrefab(UTCPreFab);
GameObject AAtile = (GameObject)Instantiate(TilePreFab);
NetworkServer.Spawn(AAtile);
IPDispOBJ.SetActive(true);
HostGameBtn.SetActive(false);
JoinGameBtn.SetActive(false);
UTCBtnOBJ.SetActive(true);
KamBtnOBJ.SetActive(true);
XydBtnOBJ.SetActive(true);
}
public void OnPressJoinGameBtn()
{
HostGameBtn.SetActive(false);
JoinBtn.SetActive(true);
IpInput.SetActive(true);
JoinGameBtn.SetActive(false);
EnterIPMsg.SetActive(true);
}
public void OnPressJoin()
{
UTCBtnIPOBJ.SetActive(true);
KamBtnIPOBJ.SetActive(true);
XydBtnIPOBJ.SetActive(true);
IpInput.SetActive(false);
JoinBtn.SetActive(false);
EnterIPMsg.SetActive(false);
}
public void OnPressTerran()
{
SelectedRace = 1;
IPDispOBJ.SetActive(false);
HostGameBtn.SetActive(false);
JoinGameBtn.SetActive(false);
KamBtnOBJ.SetActive(false);
XydBtnOBJ.SetActive(false);
Titleimg.SetActive(false);
TitleSkyImg.SetActive(false);
NetworkManager.singleton.networkAddress = "127.0.0.1";
NetworkManager.singleton.StartClient();
UTCBtnOBJ.SetActive(false);
}
public void OnPressTerranIP()
{
SelectedRace = 1;
IPDispOBJ.SetActive(false);
HostGameBtn.SetActive(false);
JoinGameBtn.SetActive(false);
KamBtnIPOBJ.SetActive(false);
XydBtnIPOBJ.SetActive(false);
Titleimg.SetActive(false);
TitleSkyImg.SetActive(false);
InputField IP = IpInput.GetComponent<InputField>();
NetworkManager.singleton.networkAddress = IP.text;
NetworkManager.singleton.StartClient();
UTCBtnIPOBJ.SetActive(false);
}
public virtual void OnServerConnect(NetworkConnection conn)
{
base.OnServerConnect(conn);
ClientScene.Ready(conn);
ClientScene.AddPlayer(conn, 1);
}
public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
if (SelectedRace == 1)
{
GameObject player = (GameObject)Instantiate(UTCPreFab);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
else if (SelectedRace == 2)
{
GameObject player = (GameObject)Instantiate(KamPreFab);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
else if (SelectedRace == 3)
{
GameObject player = (GameObject)Instantiate(XydPreFab);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
}