Skip to content

PlayerPrefab is null after defining PlayerPrefabHash  #3032

@devfox-se

Description

@devfox-se

Hi

I have a custom player prefabs where each client sends a payload of the prefab he had chosen to play with, the server has Connection Approval enabled and from there I successfully decode the data and find the appropriate prefab that should be attached to the player

The problem I am facing comes short after callback finishes execution

Specifically, issue is on this line as PlayerPrefab is null for some reason

https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/develop/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs#L741

The prefab has NetworkObject attached to it and its in Network Prefabs List

See my approval callback and how I grab the PlayerPrefabHash from my prefab

    private void ConnectionApproval(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
    {
        response.Approved = true;
        response.CreatePlayerObject = true;

        int clientOperatorType;

        try
        {
            clientOperatorType = System.BitConverter.ToInt32(request.Payload);
        }
        catch (System.Exception)
        {
            response.Approved = false;
            response.Reason = "Could not resolve OperatorType parameter.";
            return;
        }

        foreach (var data in OperatorPrefabsData.operatorPrefabPairs)
        {
            if ((int)data.operatorType == clientOperatorType)
            {
                NetworkObject netObject = data.prefab.GetComponent<NetworkObject>();
                response.PlayerPrefabHash = netObject.PrefabIdHash;
                Debug.Log(response.PlayerPrefabHash);
                break;
            }
        }

        if (response.PlayerPrefabHash == 0)
        {
            response.Approved = false;
            response.Reason = $"Could not find OperatorType {clientOperatorType}";
        }
        else
        {
            SetClientOperatorType(request.ClientNetworkId, clientOperatorType);
        }

        if (!response.Approved)
        {
            Debug.LogError($"Connection request from {request.ClientNetworkId} denied: {response.Reason}");
        }
    }

In the console I can see that response.PlayerPrefabHash is populated properly but the package fails to set the playerprefab after all.

Please advice what can be the issue in here?
Tested on both 1.8.1 and 1.11.0

Metadata

Metadata

Labels

priority:highThis issue has high priority and we are focusing to resolve itstat:importStatus - Issue is going to be saved internallytype:bugBug Report

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions