Search Unity

Question SpawnAsPlayerObject not spawning different prefabs

Discussion in 'Multiplayer' started by Lauthai, Oct 28, 2021.

  1. Lauthai

    Lauthai

    Joined:
    Mar 26, 2014
    Posts:
    4
    I am working on spawning two different player prefabs for an asymmetrical game, but when they load into the game scene, only two of the three players spawn their prefab.

    There are two prefabs the "kingPrefab" and "runnerPrefab", which are assigned in the editor. Each prefab has a NetworkObject and NetworkTransform on them. Otherwise, they contain a Character Controller and Capsule Collider along with a custom player movement script.

    On the lobby, the players will select if they are the king or runners (1 king/2 runners). Once teams are set and all players ready up, they are all transitioned via NetworkSceneManager.SwitchScene() to the game scene. However, only the two runner prefabs are spawned in.

    The king prefab is spawned in but is destroyed after only a couple frames. This can be concluded from the console either by noticing there went from 4 audio listeners in the scene to 3. Or by setting MLAPI's LogLevel to developer, you can see that There are DataHeaders with messageType = 6 which corresponds to DeleteObject.

    So I am not sure why that specific prefab is getting destroyed since I am not doing any cleanup of objects myself. It is also worth nothing that the host is clientID 0, but the second and third instances of the game are IDs 2/3 respectively. I am unable to tell what is clientID 1, but it seems to try to delete it as well inside of the console.

    This is from my ServerGameNetPortal.cs where once the start game button is clicked it calls the StartGame() method.
    Code (CSharp):
    1. public void StartGame()
    2.     {
    3.         gameInProgress = true;
    4.  
    5.         // Load the mountain level.
    6.         gameLevelLoaded = 0;
    7.         NetworkSceneManager.SwitchScene("Game-Mountain");
    8.  
    9.         // Start the round
    10.         BeginRound();
    11.     }
    12.  
    13.     // Code to handle the beginning of round logic
    14.     public void BeginRound() {
    15.         Vector3[] runnersSpawnPoints;
    16.         Vector3 kingSpawnPoint;
    17.  
    18.         // Get the spawn points for the level
    19.         switch (gameLevelLoaded) {
    20.             default:
    21.             case 0:
    22.                 runnersSpawnPoints = SpawnPoints.Instance.getRunnerSpawnPoints(gameLevelLoaded);
    23.                 kingSpawnPoint = SpawnPoints.Instance.getKingSpawnPoint(gameLevelLoaded);
    24.                 break;
    25.         }
    26.  
    27.         // Spawn in the players
    28.         int runnerSpawnIndex = 0;
    29.         foreach (PlayerData pData in clientData.Values) {
    30.             // Spawn in the prefab for the player based on king or runner
    31.             NetworkObject go = null;
    32.             if (pData.IsKing) {
    33.                 go = Instantiate(kingPrefab, kingSpawnPoint, Quaternion.identity);
    34.  
    35.             } else {
    36.                 go = Instantiate(runnerPrefab, runnersSpawnPoints[runnerSpawnIndex], Quaternion.identity);
    37.  
    38.                 // Increment the runner spawn index
    39.                 runnerSpawnIndex++;
    40.             }              
    41.  
    42.             // Spawn the player on network and assign the owner
    43.             go.SpawnAsPlayerObject(pData.ClientId);
    44.         }
    45. }
    The oddest part is that if I take both kingPrefab and runnerPrefab and set them to be the same in the editor (drag and drop for a NetworkObject field part of the script), then the same prefab will spawn. Even if I duplicate the runnerPrefab to be the kingPrefab, then all three players will spawn. But if I remove my custom player movement script from the duplicated prefab, then that player no longer spawns. That script removed has nothing to do with movement so it shouldn't affect it.

    Is it maybe a hash error where the hash of the king prefab isn't correct? I wasn't able to find any validation that would have proven this though.

    I do have a repo of the code available here on the branch "PlayerSpawning": https://github.com/Lautha1/TheKingsRace/tree/PlayerSpawning
    The current commit upon building 2 extra standalone copies to run along with the editor, will demonstrate that 1 of the 3 players do not spawn in.

    Any help is greatly appreciated, and if anything needs to be clarified I can!
     
    tobinami likes this.