Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UNET object not spawning on client

Discussion in 'UNet' started by clever, Oct 14, 2016.

  1. clever

    clever

    Joined:
    Oct 11, 2012
    Posts:
    38
    I was making a sample project to test out how SynList works when I ran into an issue that my object with NetworkIdentity/NetworkBehaviour is not even spawning on the client. Tried everything, made sure that the prefab is registered with ClientScene...etc.

    To make life easier, I included the Assets folder in this port (zip file). You can start an empty project and drop in the asset folder to test it..
    It's a simple app with one scene for the server and one for the client. The spawn object has a prefab called SyncListObjPrefab. When I hit the "Spawn Obj" button, the object spawns on the server but not the client. Any idea why? seems crazy that a simple spawn operation wouldn't work.

    PS: I prefer not using NetworkManager

    Thanks in advance for your help!

    Server code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using System;
    5.  
    6. public class TestServer : MonoBehaviour {
    7.  
    8.     public int port;
    9.     public GameObject syncListObjPrefab;
    10.  
    11.     string onscreenlog;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         onscreenlog = "";
    16.     }
    17.  
    18.     public void Initialize(int port)
    19.     {
    20.  
    21.         var config = new ConnectionConfig();
    22.         config.AddChannel(QosType.ReliableSequenced);
    23.         config.AddChannel(QosType.Unreliable);
    24.         config.AddChannel(QosType.ReliableSequenced); // control channel (2) (game interactions)
    25.         config.MinUpdateTimeout = 1;
    26.      
    27.      
    28.         NetworkServer.Configure(config, 6000);
    29.  
    30.         // do we need this in the server code??
    31.         ClientScene.RegisterPrefab(syncListObjPrefab);
    32.      
    33.         NetworkServer.Listen(port);
    34.      
    35.         NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
    36.         NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
    37.    
    38.  
    39.         Debug.Log("Server initialized on " + port + " " + DateTime.UtcNow.ToString());
    40.  
    41.     }
    42.  
    43.     void OnConnected(NetworkMessage netMsg)
    44.     {
    45.  
    46.         onscreenlog += "\nconnection from " + netMsg.conn.connectionId + " // " + netMsg.conn.address + " @ " + DateTime.UtcNow.ToString();
    47.         Debug.Log("connection from " + netMsg.conn.connectionId + " // " + netMsg.conn.address + " @ " + DateTime.UtcNow.ToString());
    48.     }
    49.  
    50.     void OnDisconnected(NetworkMessage netMsg)
    51.     {
    52.         onscreenlog += "\ndisconnected " + netMsg.conn.connectionId + " // " + netMsg.conn.address + " @ " + DateTime.UtcNow.ToString();
    53.         Debug.Log("disconnected " + netMsg.conn.connectionId + " // " + netMsg.conn.address + " @ " + DateTime.UtcNow.ToString());
    54.     }
    55.  
    56.     public void Disconnect()
    57.     {
    58.         NetworkServer.DisconnectAll();
    59.         NetworkServer.Shutdown();
    60.     }
    61.  
    62.     public void SpawnObj()
    63.     {
    64.         GameObject obj = (GameObject)Instantiate(syncListObjPrefab);
    65.  
    66.         NetworkServer.Spawn(obj);
    67.  
    68.         onscreenlog += "\nspawned objs = "+ NetworkServer.objects.Count;
    69.        Debug.Log("object spawned: spawn count=" + NetworkServer.objects.Count);
    70.     }
    71.  
    72.     void OnGUI () {
    73.  
    74.         GUILayout.Label("Server started ? " + NetworkServer.active);
    75.  
    76.         GUILayout.Label("Port:");
    77.         port = int.Parse( GUILayout.TextField(port.ToString(), GUILayout.Width(200f)) );
    78.  
    79.         if (!NetworkServer.active && GUILayout.Button("CONNECT"))
    80.         {
    81.             Initialize(port);
    82.             Debug.Log("UR_Server initialized");
    83.         }
    84.  
    85.         if (NetworkServer.active && GUILayout.Button("DISCONNECT"))
    86.         {
    87.             Disconnect();
    88.             Debug.Log("UR_Server disconnected");
    89.         }
    90.  
    91.         GUILayout.Space(75f);
    92.  
    93.         if (NetworkServer.active && GUILayout.Button("SPAWN OBJ"))
    94.         {
    95.             SpawnObj();
    96.         }
    97.  
    98.         GUILayout.Space(75f);
    99.  
    100.         GUILayout.Label(onscreenlog);
    101.     }
    102. }
    103.  
    Client Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6. using UnityEngine.Networking.NetworkSystem;
    7. using System.Text;
    8.  
    9. public class TestClient : MonoBehaviour {
    10.  
    11.     public GameObject SyncListObjPrefab;
    12.  
    13.     public int port;
    14.     public string ip;
    15.  
    16.     NetworkClient client;
    17.  
    18.     string onScreenLog;
    19.  
    20.  
    21.     void ConnectClient()
    22.     {
    23.  
    24.  
    25.         var config = new ConnectionConfig();
    26.         config.AddChannel(QosType.ReliableSequenced);
    27.         config.AddChannel(QosType.Unreliable);
    28.         config.AddChannel(QosType.ReliableSequenced); // control channel (2) (game interactions)
    29.         config.MinUpdateTimeout = 1;
    30.  
    31.  
    32.  
    33.         client.Configure(config, 6000);
    34.  
    35.         ClientScene.RegisterPrefab(SyncListObjPrefab);
    36.  
    37.         System.Net.IPAddress serverIP;
    38.         if (System.Net.IPAddress.TryParse(ip, out serverIP))
    39.         {
    40.             System.Net.EndPoint endPoint = new System.Net.IPEndPoint(serverIP, port);
    41.  
    42.             client.Connect(endPoint);
    43.  
    44.             Debug.Log(endPoint.ToString() + " " + endPoint.AddressFamily.ToString());
    45.  
    46.             onScreenLog += (endPoint.ToString() + " / " + endPoint.AddressFamily.ToString() + Environment.NewLine);
    47.         }
    48.         else
    49.         {
    50.             client.Connect(ip, port);
    51.  
    52.             Debug.Log("connect " + ip + ":" + port);
    53.             onScreenLog += "connect " + ip + ":" + port;
    54.  
    55.         }
    56.  
    57.  
    58.  
    59.         client.RegisterHandler(MsgType.Connect, OnConnected);
    60.         client.RegisterHandler(MsgType.Error, OnError);
    61.      
    62.  
    63.     }
    64.  
    65.     void OnError(NetworkMessage netMsg)
    66.     {
    67.         var errorMsg = netMsg.ReadMessage<ErrorMessage>();
    68.         Debug.Log("Error:" + errorMsg.errorCode);
    69.     }
    70.  
    71.     void OnConnected(NetworkMessage netMsg)
    72.     {
    73.  
    74.         onScreenLog += "\nconnected..";
    75.         Debug.Log("spawnable = " + ClientScene.prefabs.Count);
    76.         //spawner.OnStartClient();
    77.  
    78.         Debug.Log("connected");
    79.     }
    80.  
    81.  
    82.     // Use this for initialization
    83.     void Start () {
    84.         client = new NetworkClient();
    85.         onScreenLog = "----\n";
    86.     }
    87.  
    88.  
    89.     void OnGUI () {
    90.  
    91.         GUILayout.Label("Client status : connected=" + client.isConnected);
    92.  
    93.         ip = GUILayout.TextField(ip, GUILayout.Width(300f));
    94.         port = int.Parse(GUILayout.TextField(port.ToString(), 5, GUILayout.Width(150f)));
    95.  
    96.         GUILayout.Space(30f);
    97.  
    98.         if (!client.isConnected && GUILayout.Button("CONNECT"))
    99.         {
    100.             ConnectClient();
    101.         }
    102.  
    103.         if (client.isConnected && GUILayout.Button("DISCONNECT"))
    104.         {
    105.             client.Disconnect();
    106.         }
    107.  
    108.         GUILayout.Space(100f);
    109.         GUILayout.Label(onScreenLog);
    110.     }
    111. }
    112.  
    And finally this is the script attached to the Spawn object :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public struct MyStruct
    6. {
    7.     public int id;
    8.     public string name;
    9. }
    10.  
    11. public class SyncListObj : NetworkBehaviour {
    12.  
    13.     [SerializeField]
    14.     public SyncListRooms _roomList = new SyncListRooms();
    15.  
    16.     public class SyncListRooms : SyncListStruct<MyStruct> { }
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.     }
    27. }
    28.  
     

    Attached Files:

    Last edited: Oct 15, 2016
  2. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Hi,
    Rather than posting a zip, please post the code using the code tags for ease of viewing.
     
  3. clever

    clever

    Joined:
    Oct 11, 2012
    Posts:
    38
    Will do. Thought that this way if the problem is in the scene rather than the script I would get feedback
     
  4. clever

    clever

    Joined:
    Oct 11, 2012
    Posts:
    38
    Not sure why assistance is so slow when it comes to UNET questions :( (I have another question in the forum with 0 replies too...)

    Anyways, I found the answer, objects will only spawn if the client is "Ready"

    so if anyone out there is having this problem, then you need to do this in your server:

    NetworkServer.SetClientReady(playerConnection);

    You could do so in the OnConnect callback if you want players to have Spawn objects appear immediately.