Client Error: Failed to spawn server object

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()"

private void OnServerConnect( NetworkMessage networkMessage )
    {
    ClientScene.RegisterPrefab( prefabPlayer );
    GameObject thePlayer = (GameObject)Instantiate( prefabPlayer, new Vector3( 0, 0, 0 ), Quaternion.identity );
    NetworkServer.AddPlayerForConnection( networkMessage.conn, thePlayer, 0 );
    }

The player Prefab has a network identity with local player auth checked

What am i missing client side?

the player prefab is not registered with the spawning system. It must be in a slot on the NetworkManager or registered with ClientScene.RegisterPrefab().

6 Likes

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:

public class MultiplayerRoom : NetworkLobbyManager
{
    // ... omitted code here that had to do with creating a lobby
 
    /// <summary>
    /// Connects to the lobby
    /// </summary>
    public void ConnectToRoom(GameObject gameList, MatchDesc match)
    {
        networkMatch = gameObject.AddComponent<NetworkMatch>();
        gameList.SetActive(false);
        networkMatch.JoinMatch(match.networkId, "", OnMatchJoined);    // connect to the room
    }

    /// <summary>
    /// When a lobby has been joined
    /// </summary>
    /// <param name="matchJoin">Result of joining a lobby</param>
    public void OnMatchJoined(JoinMatchResponse matchJoin)
    {
        if (matchJoin.success)
        {
            Debug.Log("Join match succeeded");
      
            Utility.SetAccessTokenForNetwork(matchJoin.networkId, new NetworkAccessToken(matchJoin.accessTokenString));
            NetworkClient myClient = new NetworkClient();
            myClient.RegisterHandler(MsgType.Connect, OnConnected);
            myClient.Connect(new MatchInfo(matchJoin));
            int id = NetworkClient.allClients[0].connection.playerControllers.Count;
      
            ClientScene.AddPlayer(myClient.connection, (short) id);
        }
        else
        {
            Debug.LogError("Join match failed");
        }
    }

    /// <summary>
    /// When connected to the lobby
    /// </summary>
    /// <param name="msg">message</param>
    public void OnConnected(NetworkMessage msg)
    {
        Debug.Log("OnConnected: " + msg);
    }
}

I’m still stuck at this problem.

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.

11 Likes

Great ,thanks

If not using drag to register objects for spawning,using code how to do ?

1 Like

A little late to the party, but;

gameObject.GetComponent().spawnPrefabs.Add(yourPrefab);

1 Like

Hey, I have both dragged the prefabs into the inspector and registered it in networkmanager script but neither works.

public class NetworkManager : Mirror.NetworkManager
{
    public override void OnStartClient()
    {
        var spawnablePrefabs = Resources.LoadAll<GameObject>("Assets/Prefabs/Spawnable");

        for (int i = 0; i < spawnablePrefabs.Length; i++)
            ClientScene.RegisterPrefab(spawnablePrefabs[i]);
    }

    public override void OnStartServer()
        => spawnPrefabs = Resources.LoadAll<GameObject>("Assets/Prefabs/Spawnable").ToList();
}

Suggestions?

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.

6623329--754690--Capture.PNG

Hope this helps :slight_smile:

2 Likes