Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Server and Client connecting but telling me I am not connected when I try to instantiate

Discussion in 'Multiplayer' started by tylerreece22, Nov 25, 2015.

  1. tylerreece22

    tylerreece22

    Joined:
    Jul 4, 2015
    Posts:
    11
    So everything seems to work fine when I am simply connecting the server and client. I even put debugging code in the methods but when I try to instantiate the prefab the client says it cant do that because it is not connected.

    Thanks for the help ahead of time! :)

    Code (csharp):
    1. public void SetupServer()
    2.     {
    3.         NetworkServer.Listen(4444);
    4.         isAtStartup = false;
    5.         int x = 0;
    6.         while (x == 0)
    7.         {
    8.             if (NetworkServer.active)
    9.             {
    10.                 Debug.Log("Server Connected");
    11.                 x = 1;
    12.             }
    13.         }
    14.     }
    15.  
    16.     // Create a client and connect to the server port
    17.     public void SetupClient()
    18.     {
    19.         myClient = new NetworkClient();
    20.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    21.         myClient.Connect("127.0.0.1", 4444);
    22.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    23.         ClientScene.RegisterPrefab(playerPrefab);
    24.  
    25.         isAtStartup = false;      
    26.     }
    27.  
    28.     // Create a local client and connect to the local server
    29.     public void SetupLocalClient()
    30.     {
    31.         myClient = ClientScene.ConnectLocalServer();
    32.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    33.         isAtStartup = false;
    34.     }
    35.  
    36.     // client function
    37.     public void OnConnected(NetworkMessage netMsg)
    38.     {
    39.         Debug.Log("Connected to server");
    40.         int x = 0;
    41.         while (x == 0)
    42.         {
    43.             if (myClient.isConnected)
    44.             {
    45.                 Network.Instantiate(playerPrefab, new Vector3(6f, 0.6f, 4.5f), Quaternion.identity, 0);
    46.                 x = 1;
    47.             }
    48.         }
    49.     }
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Network.Instantiate is from the legacy network API, not from UNet..
     
  3. tylerreece22

    tylerreece22

    Joined:
    Jul 4, 2015
    Posts:
    11
    Okay cool. I switched it to NetworkManager.instantiate() and now it spawns the player but it doesn't communicate the player's position and data to the server. Any idea why that is?
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
  5. tylerreece22

    tylerreece22

    Joined:
    Jul 4, 2015
    Posts:
    11
    Thanks so much! I will definitely try this!!! Would you mind if I stayed in contact with you in case I have any other questions?