Skip to content

Spawn fails randomly because AssetPathHash/PrefabId is wrong #995

@thecrazycraft

Description

@thecrazycraft

Unity 6000.3.2f1
4.6.18R Pro
https://discord.com/channels/424284635074134018/1034477094731784302/1457778528484462672

Description:

My game uses the following script to spawn the player:

    public class ScenePlayerSpawner : NetworkBehaviour
    {
        [Tooltip("Prefab to spawn for the player.")]
        [SerializeField]
        private NetworkObject _playerPrefab;
        
        public override void OnSpawnServer(NetworkConnection connection)
        {
            StartCoroutine(SpawnPlayer(connection));
        }

        private IEnumerator SpawnPlayer(NetworkConnection connection)
        {
            yield return new WaitUntil(() => IsServerStarted);
            yield return new WaitUntil(connection.LoadedStartScenes);
            
            Debug.Log($"[PlayerSpawner] Spawning object: {_playerPrefab.PrefabId} and {_playerPrefab.SpawnableCollectionId} and {_playerPrefab.AssetPathHash} and {_playerPrefab.name}");
            var nob = NetworkManager.GetPooledInstantiated(_playerPrefab, transform.position, transform.rotation, true);
            NetworkManager.ServerManager.Spawn(nob, connection);
            Debug.Log($"[PlayerSpawner] Spawned object: {nob.PrefabId} and {nob.SpawnableCollectionId} and {nob.AssetPathHash} and {nob.name}");
        }
    }

Somtimes, when starting as host in a build, another prefab is spawned instead of the player. This other prefab is the first prefab in DefaultPrefabObjects with PrefabId 0.

Reserializing the prefabs does not help.

I confirmed that SpawnablePrefabs.InitializePrefabRange(0); in line 308 of the NetworkManager executes.

This is a screenshot from when the debug code only contained the AssetPathHash and the issue happened.
Image

I'm not able to reproduce the issue right now, but last time it happened it showed:
[PlayerSpawner] Spawning object: 0 and 0 and AssetPathHash of player and NetworkPlayer
[PlayerSpawner] Spawned object: 0 and 0 and AssetPathHash of some random weapon attachment that is first in DefaultPrefabObjects and name of said attachment.

Replication:

The issue is non deterministic and I found nothing that influences it, besides removing every other prefab and making the player the only network prefab.

The issue only happens in-build and seems to be tied to the build. Some builds never work, some builds always work. I tried on my PC and Mac and both are equally inconsistent. When I build over and over again without changing anything, the result remains the same. But when I change something (can be completely unrelated), the result might change.

Expectation:

I expect FishNet to spawn the correct object and assign the prefab ids correctly.

This is what the debug statements look like when it works:
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions