Search Unity

Client Error: Failed to spawn server object

Discussion in 'Multiplayer' started by rob_vld, Jun 24, 2015.

  1. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    HLAPI, C#

    When a client connects to the server, the server spawns the gameObject... at the client however i get a "
    Failed to spawn server object, assetId=532ba720a948274498667f4619a439 netId=1
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()"

    Code (CSharp):
    1. private void OnServerConnect( NetworkMessage networkMessage )
    2.     {
    3.     ClientScene.RegisterPrefab( prefabPlayer );
    4.     GameObject thePlayer = (GameObject)Instantiate( prefabPlayer, new Vector3( 0, 0, 0 ), Quaternion.identity );
    5.     NetworkServer.AddPlayerForConnection( networkMessage.conn, thePlayer, 0 );
    6.     }
    The player Prefab has a network identity with local player auth checked

    What am i missing client side?
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    the player prefab is not registered with the spawning system. It must be in a slot on the NetworkManager or registered with ClientScene.RegisterPrefab().
     
  3. Gatau

    Gatau

    Joined:
    Feb 20, 2015
    Posts:
    9
    Hi, I am having the same problem but I don't understand seanr's reply or I have done something else wrong because my NetworkLobbyManager has its lobby player and game player prefabs dragged to in the editor.

    When I join, the client that is not the server gets that message two times and my code for joninig looks like this:

    Code (CSharp):
    1.  
    2. public class MultiplayerRoom : NetworkLobbyManager
    3. {
    4.     // ... omitted code here that had to do with creating a lobby
    5.  
    6.     /// <summary>
    7.     /// Connects to the lobby
    8.     /// </summary>
    9.     public void ConnectToRoom(GameObject gameList, MatchDesc match)
    10.     {
    11.         networkMatch = gameObject.AddComponent<NetworkMatch>();
    12.         gameList.SetActive(false);
    13.         networkMatch.JoinMatch(match.networkId, "", OnMatchJoined);    // connect to the room
    14.     }
    15.  
    16.     /// <summary>
    17.     /// When a lobby has been joined
    18.     /// </summary>
    19.     /// <param name="matchJoin">Result of joining a lobby</param>
    20.     public void OnMatchJoined(JoinMatchResponse matchJoin)
    21.     {
    22.         if (matchJoin.success)
    23.         {
    24.             Debug.Log("Join match succeeded");
    25.      
    26.             Utility.SetAccessTokenForNetwork(matchJoin.networkId, new NetworkAccessToken(matchJoin.accessTokenString));
    27.             NetworkClient myClient = new NetworkClient();
    28.             myClient.RegisterHandler(MsgType.Connect, OnConnected);
    29.             myClient.Connect(new MatchInfo(matchJoin));
    30.             int id = NetworkClient.allClients[0].connection.playerControllers.Count;
    31.      
    32.             ClientScene.AddPlayer(myClient.connection, (short) id);
    33.         }
    34.         else
    35.         {
    36.             Debug.LogError("Join match failed");
    37.         }
    38.     }
    39.  
    40.     /// <summary>
    41.     /// When connected to the lobby
    42.     /// </summary>
    43.     /// <param name="msg">message</param>
    44.     public void OnConnected(NetworkMessage msg)
    45.     {
    46.         Debug.Log("OnConnected: " + msg);
    47.     }
    48. }
     
    Last edited: Jul 27, 2015
  4. Gatau

    Gatau

    Joined:
    Feb 20, 2015
    Posts:
    9
    I'm still stuck at this problem.
     
  5. vvnurmi

    vvnurmi

    Joined:
    Dec 25, 2015
    Posts:
    1
    Gatau, check this piece of documentation about spawning: http://docs.unity3d.com/Manual/UNetSpawning.html

    To register objects for spawning, the easiest way is to drag them to the Network Manager in the Inspector window. The attached screenshot is taken from Unity 5.3. spawn-register.png
     
  6. ramp

    ramp

    Joined:
    Nov 6, 2012
    Posts:
    14
    Great ,thanks
     
  7. Yellow_Dog

    Yellow_Dog

    Joined:
    Apr 13, 2017
    Posts:
    1
    If not using drag to register objects for spawning,using code how to do ?
     
    klesun likes this.
  8. paulsendroiu

    paulsendroiu

    Joined:
    Mar 12, 2019
    Posts:
    6
    A little late to the party, but;

    gameObject.GetComponent<NetworkManager>().spawnPrefabs.Add(yourPrefab);
     
  9. kentzeus

    kentzeus

    Joined:
    Mar 1, 2020
    Posts:
    9
    Hey, I have both dragged the prefabs into the inspector and registered it in networkmanager script but neither works.

    Code (CSharp):
    1. public class NetworkManager : Mirror.NetworkManager
    2. {
    3.     public override void OnStartClient()
    4.     {
    5.         var spawnablePrefabs = Resources.LoadAll<GameObject>("Assets/Prefabs/Spawnable");
    6.  
    7.         for (int i = 0; i < spawnablePrefabs.Length; i++)
    8.             ClientScene.RegisterPrefab(spawnablePrefabs[i]);
    9.     }
    10.  
    11.     public override void OnStartServer()
    12.         => spawnPrefabs = Resources.LoadAll<GameObject>("Assets/Prefabs/Spawnable").ToList();
    13. }
    Suggestions?
     
    Last edited: Jul 13, 2020
  10. Flosener

    Flosener

    Joined:
    Apr 11, 2020
    Posts:
    1
    So I guess I figured it out. On your to-be-spawned object check the asset ID (in the inspector at the bottom open "network information") and if it is the same on both devices you are trying to spawn on. It is probably different which is why the host can spawn the objects, the client cannot. To repair this so both have the same asset ID you probably have to rebuild your application / push and pull from your project again to be on the same page with both devices.

    Capture.PNG

    Hope this helps :)
     
    kostadinowww and DaneyT like this.