Search Unity

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

Discussion in 'Multiplayer' started by AllTheGoodNamesWereTaken, Oct 26, 2015.

  1. AllTheGoodNamesWereTaken

    AllTheGoodNamesWereTaken

    Joined:
    Sep 24, 2015
    Posts:
    14
    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 ☑], most of the Object Creation Flow should be taken care of.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class MyNetworkManager : NetworkManager {
    5.     private GameObject playerBall; //prefab (registered on MyNetworkManager GUI component)
    6.  
    7.     public void ChooseBall(GameObject selectedBall){ //Button onClick passes in our player prefab
    8.         playerBall = selectedBall;  //select our player prefab
    9.         Application.LoadLevel("FightScene"); //load next level [Don't Destroy On Load ☑]
    10.     }
    11.  
    12.     void OnLevelWasLoaded(int level){  //when next level is loaded...
    13.         if (level == 1) GetComponent<NetworkManagerHUD>().showGUI = true; //...show network HUD
    14.     }
    15.  
    16.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    17.     {
    18.         Transform startingPosition = GetStartPosition(); //Finds a spawn position based on NetworkStartPosition objects in the scene, used by the default implementation of OnServerAddPlayer.
    19.         GameObject player = (GameObject)Instantiate(playerBall, startingPosition.position, startingPosition.rotation);
    20.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    21.     }
    22. }
    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!

    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()
     
    Last edited: Oct 29, 2015
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Are you changing scenes?
     
  3. AllTheGoodNamesWereTaken

    AllTheGoodNamesWereTaken

    Joined:
    Sep 24, 2015
    Posts:
    14
    Yes. Scene 0 has a button to select the player prefab. Scene 1 is then loaded. Network HUD is then shown.
     
  4. Buscatrufas

    Buscatrufas

    Joined:
    Apr 28, 2015
    Posts:
    4
    I have the same problem mate, i up your thread and wait a answer , good luck!
     
  5. AllTheGoodNamesWereTaken

    AllTheGoodNamesWereTaken

    Joined:
    Sep 24, 2015
    Posts:
    14
    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?
     
    jister likes this.
  6. woodsynzl

    woodsynzl

    Joined:
    Apr 8, 2015
    Posts:
    156
  7. jenkins22

    jenkins22

    Joined:
    Nov 15, 2014
    Posts:
    10
    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.
     
  8. jenkins22

    jenkins22

    Joined:
    Nov 15, 2014
    Posts:
    10
    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!
     
    stonecompass and Jean-Fabre like this.
  9. stonecompass

    stonecompass

    Joined:
    Aug 5, 2015
    Posts:
    13
    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?
     
  10. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    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.
     
    isidro02139 likes this.
  11. mojtaba64

    mojtaba64

    Joined:
    Aug 3, 2010
    Posts:
    56
    I have the same problem.
    It just don't work..

    This code is in my custome network manager:
    Code (CSharp):
    1. public class MyNetworkManager : NetworkManager
    2. {
    3.     public GameObject testobj;
    4.  
    5.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    6.     {
    7.         ClientScene.RegisterPrefab(testobj);
    8.         GameObject thePlayer = (GameObject)Instantiate(testobj, Vector3.zero, Quaternion.identity);
    9.         NetworkServer.AddPlayerForConnection(conn, thePlayer, playerControllerId);
    10.     }
    11. }