Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Mirror: ReplacePlayerForConnection causes crashes

Discussion in 'Multiplayer' started by protomor, Oct 10, 2023.

  1. protomor

    protomor

    Joined:
    Dec 13, 2021
    Posts:
    34
    Long story short, I swear I had this code working and then I went on a work trip for 2 weeks. I'm using ParrelSync to run two instances and the out of the box Mirror HUD to connect them together.

    The error I get is:
    Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=1513053432 netId=2

    I'm triyng to get players to connect as a player or a vehicle. Per googling, I made a base prefab (see code below) that swaps out the default prefab with one of two prefabs. I swear this worked prior to my trip but now, the server Unity instance will freeze and the client will give the above error. All prefabs have a network behavior and a network transform. These also work if I don't do my "ghost" prefab and do either spectator or vehicle prefab.
    Code (CSharp):
    1. public class GhostNetworkManager : NetworkBehaviour
    2. {
    3.     bool DidSpawn = false;
    4.     void Start()
    5.     {
    6.     }
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         if (!DidSpawn && NetworkManager.singleton.isNetworkActive)
    12.         {
    13.             SpawnCharacter();
    14.         }
    15.     }
    16.  
    17.     [Command]
    18.     public void SpawnCharacter()
    19.     {
    20.         GameObject[] mainCameras = GameObject.FindGameObjectsWithTag("MainCamera");
    21.  
    22.         foreach (GameObject cameraObject in mainCameras)
    23.         {
    24.             cameraObject.SetActive(false);
    25.         }
    26.  
    27.         if (CommandLineArguments.Instance.IsSpectator)
    28.         {
    29.             var playerprefab = NetworkManager.singleton.playerSpectatorPrefab;
    30.             var newPlayer = Instantiate<GameObject>(playerprefab);
    31.             NetworkServer.ReplacePlayerForConnection(connectionToClient, newPlayer, true);
    32.             PlayerInput playerInput = playerprefab.GetComponent<PlayerInput>();
    33.             playerInput.enabled = true;
    34.             CharacterController characterController = playerprefab.GetComponent<CharacterController>();
    35.             characterController.enabled = true;
    36.             GameObject[] PlayerFollowCamera = GameObject.FindGameObjectsWithTag("PlayerFollowCamera");
    37.             PlayerFollowCamera[0].SetActive(true);
    38.         }
    39.         else
    40.         {
    41.             var newPlayer = Instantiate<GameObject>(NetworkManager.singleton.playerVehiclePrefab);
    42.             NetworkServer.ReplacePlayerForConnection(connectionToClient, newPlayer, true);
    43.         }
    44.  
    45.         DidSpawn = true;
    46.     }
    47. }
    Probably unrelated, but I started getting these errors as well on startup:

    Unable to add localization key: 'UserNotFoundSignUp': Requested value 'UserNotFoundSignUp' was not found.
    Unable to add localization key: 'VisitUnityDashboardToSignUp': Requested value 'VisitUnityDashboardToSignUp' was not found.
     
  2. protomor

    protomor

    Joined:
    Dec 13, 2021
    Posts:
    34
    For anyone in the future, the answer was that I never registered the game object with "spawnableobjects" in the network manager
     
    CodeNinja- likes this.