Okay, so I’m doing something wrong. My playerdata prefab gets loaded on both the host and client when I use Unity’s prebuild HUB. But with mine, only the host gets the player data and the client doesn’t even see it.
I’m confused on how there are two different ways of hosting a server x.x
I’m also getting an error for “ClientScene.Ready (nwManager.client.connection);” It says it is a null reference. Any help would be appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using TMPro;
public class MyNetworkScript : MonoBehaviour {
public GameObject guestButton;
public GameObject tmp;
TextMeshProUGUI tmpText;
NetworkClient client;
NetworkManager nwManager;
// Initalize everything.
void Start ()
{
guestButton.SetActive (true);
tmpText = tmp.GetComponent<TextMeshProUGUI> ();
tmpText.text = "Login";
// Sets the client and network variables.
client = new NetworkClient ();
nwManager = this.GetComponent<NetworkManager> ();
// Setup detection for connect and disconnect.
client.RegisterHandler (MsgType.Disconnect, OnFailedToConnect);
client.RegisterHandler (MsgType.Connect, OnConnected);
}
// This gets called when client connects.
void OnConnected (NetworkMessage msg)
{
Debug.Log ("Connected!!");
tmpText.text = "Connected!";
Debug.Log (ClientScene.ready);
Debug.Log (NetworkClient.active);
if (NetworkClient.active && !ClientScene.ready)
{
ClientScene.Ready (nwManager.client.connection);
if (ClientScene.localPlayers.Count == 0)
{
ClientScene.AddPlayer (0);
}
}
}
public void ClientConnection ()
{
client.Connect ("127.0.0.1", 7777);
guestButton.SetActive (false);
tmpText.text = "Connecting...";
}
void OnFailedToConnect (NetworkMessage error)
{
Debug.Log ("Failed to connect or lost connection.");
nwManager.StartHost ();
//nwManager.StartServer ();
client.Connect ("127.0.0.1", 7777);
}
}