"Failed to spawn server object" client player prefabs not showing up.

From what I’ve read, to use a custom player object you simply need to override the OnServerAddPlayer() in your network manager. As long as [Auto Create Player :ballot_box_with_check:], most of the Object Creation Flow should be taken care of.

using UnityEngine;
using UnityEngine.Networking;

public class MyNetworkManager : NetworkManager {
    private GameObject playerBall; //prefab (registered on MyNetworkManager GUI component)

    public void ChooseBall(GameObject selectedBall){ //Button onClick passes in our player prefab
        playerBall = selectedBall;  //select our player prefab
        Application.LoadLevel("FightScene"); //load next level [Don't Destroy On Load ☑]
    }

    void OnLevelWasLoaded(int level){  //when next level is loaded...
        if (level == 1) GetComponent<NetworkManagerHUD>().showGUI = true; //...show network HUD
    }

    public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    {
        Transform startingPosition = GetStartPosition(); //Finds a spawn position based on NetworkStartPosition objects in the scene, used by the default implementation of OnServerAddPlayer.
        GameObject player = (GameObject)Instantiate(playerBall, startingPosition.position, startingPosition.rotation);
        NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    }
}

What happens: I select Host, playerBall is spawned. In another instance of the game I join as client, playerBall is spawned on the server. Host sees 2 playerBalls, Client sees none.

Failed to spawn server object, assetId=52db34a96abef44bb66c11050d21c39 netId=4
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

My Question: Why are the playerBall’s not showing up on the client? How should I debug something like this?
For anyone kind enough to reply, Thanks in advance!

On a side note

Once in a great while they both spawn correctly, and I can control both of them from their respective game instances. It is how ever very laggy. Most of the time the client simply freezes up then stops. I also occasionally get the following errors:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkScene.RegisterPrefab (UnityEngine.GameObject prefab) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkScene.cs:108)
UnityEngine.Networking.ClientScene.RegisterPrefab (UnityEngine.GameObject prefab) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:336)
MyNetworkManager.Start () (at Assets/Title/MyNetworkManager.cs:8)

UNet Server Disconnect Error: BadMessage conn:[hostId: 0 connectionId: 1 isReady: True channel count: 2]:1
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

Are you changing scenes?

Yes. Scene 0 has a button to select the player prefab. Scene 1 is then loaded. Network HUD is then shown.

I have the same problem mate, i up your thread and wait a answer , good luck!

I tried to simplify this by making a new project with just one test scene. I’ve been trying everything I can think of for the past couple of weeks. I just want to spawn either a SquarePrefab or a SpherePrefab. Just making 2 different prefabs spawn seems impossible (at least just by simply overriding the OnServerAddPlayer method). It says in the API “The playerPrefab on the NetworkManager does not have to be used to create player objects. You could use different methods of creating different players.” But this does not seem possible using the HLAPI. Any advice?

1 Like

I’m following this thread! This is what I want to implement into my project but I’m too noob to do so! Changing Vehicle Prefab At Current Position - Unity Engine - Unity Discussions

I know it has been a while, but did you manage to figure this out? I’m getting the same error (though in a slightly different context). The Player prefab is correctly spawned on the server, but on the remote client that is connecting, I get the same error message and no Player spawned.

Well, I’ve JUST figured it out after 2 days of investigation. All I needed to do in my case was to call ClientScene.RegisterPrefab(myPlayerPrefab) in the Client code before attempting to spawn anything. Hope this helps someone!

2 Likes

I’ve been having the same issues. Did you call the RegisterPrefab-method in OnClientConnect to get it to work or where did you call it?

I’ve found this behavior when loading the NetworkManager as part of a scene out of an AssetBundle in 5.4.0f3 and have reported the bug. Shifting the NetworkManager outside of the bundle worked around the problem for me.

1 Like

I have the same problem.
It just don’t work…

This code is in my custome network manager:

public class MyNetworkManager : NetworkManager
{
    public GameObject testobj;

    public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    {
        ClientScene.RegisterPrefab(testobj);
        GameObject thePlayer = (GameObject)Instantiate(testobj, Vector3.zero, Quaternion.identity);
        NetworkServer.AddPlayerForConnection(conn, thePlayer, playerControllerId);
    }
}