- Home /
Question by
MadDevil · Mar 28, 2018 at 07:38 AM ·
networkingmultiplayernetworkmultiplayer-networking
Spawn Cube doesnt Show up on client. (Unity Networking)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class NetworkManagerScript : NetworkDiscovery {
// setup a client, wait for sometime if there is no server found
// setup a server
// connect the client and server.
public GameObject cube;
NetworkClient myClient;
bool isConnectedToServer;
public Text msg;
IEnumerator Start () {
Application.runInBackground = true;
SetUpClient ();
yield return new WaitForSeconds (10f);
if (!isConnectedToServer)
SetUpServer ();
ClientScene.RegisterPrefab (cube);
}
public void SetUpClient()
{
Debug.Log ("Starting as client");
Initialize ();
StartAsClient ();
}
public override void OnReceivedBroadcast(string fromAddress, string data)
{
if (isConnectedToServer)
return;
Debug.Log("Server Found");
msg.text = "Server Found";
string _ip = fromAddress;
int _port = int.Parse (data);
msg.text = "broadcast recieved. network client";
myClient = new NetworkClient ();
myClient.RegisterHandler (MsgType.Connect, OnConnected);
myClient.Connect (_ip, _port);
}
void SpawnOnClient(NetworkMessage netMsg)
{
//ClientScene.spawnableObjects
msg.text = "Object spawned on server.";
}
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log ("Connected to server.");
isConnectedToServer = true;
msg.text = "Connected to server.";
myClient.RegisterHandler (MsgType.ObjectSpawn, SpawnOnClient);
// StartAsClient ();
// StartCoroutine (SpawnCube());
}
public void OnClientConnected(NetworkMessage netMsg)
{
Debug.Log ("Client connected to server.");
//isConnectedToServer = true;
msg.text = "Client connected to server.";
StartCoroutine (SpawnCube());
}
IEnumerator SpawnCube()
{
yield return new WaitForSeconds (2f);
GameObject obj = GameObject.Instantiate(cube);
NetworkServer.Spawn(obj);
//Transform go = (Transform)Network.Instantiate (cube,Vector3.zero,Quaternion.identity,0);
//cube.SetActive(true);
yield return null;
//NetworkServer.Spawn(cube);
yield return null;
// while (true) {
// go.position = new Vector3 (Random.Range(-5.0f,5.0f), 0f, Random.Range(-5.0f,5.0f));
// yield return new WaitForSeconds (3f);
// }
}
public void SetUpServer()
{
int serverPort = createServer();
if (serverPort != -1)
{
Debug.Log("Server created on port : " + serverPort);
NetworkServer.RegisterHandler(MsgType.Connect, OnClientConnected);
broadcastData = serverPort.ToString();
Initialize ();
StartAsServer();
msg.text = "Server Created on port : " + serverPort;
}
else
{
Debug.Log("Failed to create Server");
msg.text = "Failed to create server.";
}
}
int minPort = 10000;
int maxPort = 10010;
int defaultPort = 10000;
int createServer()
{
int serverPort = -1;
bool serverCreated = NetworkServer.Listen(defaultPort);
if (serverCreated)
{
serverPort = defaultPort;
}
else
{
Debug.Log("Failed to create with the default port");
msg.text = "Failed to create with the default port";
for (int tempPort = minPort; tempPort <= maxPort; tempPort++)
{
if (tempPort != defaultPort)
{
if (NetworkServer.Listen(tempPort))
{
serverPort = tempPort;
break;
}
if (tempPort == maxPort)
{
Debug.LogError("Failed to create server.");
msg.text = "Failed to create server.";
}
}
}
}
return serverPort;
}
void GetServerStats()
{
Debug.Log (NetworkServer.connections.Count);
}
}
I am trying to spawn a cube by using NetworkServer.Spawn(obj) in the registercallback when the client gets connected to the server. The cube has network identity attached to it and it Instantiates on the host side but it doest show on the client side.
I am pulling my hair off trying to figure this out from last 2 days, neither google or the documentation is helping, so any kind of help will be greatly appreciated.
Thanks, Murtaza
Comment