Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to obtain PlayerPrefabHash from prefabs

Discussion in 'Netcode for GameObjects' started by Suduckgames, Oct 25, 2022.

  1. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    218
    I am trying to spawn a specific prefab for a player and the documentation states the following:

    PlayerPrefabHash: The type of player prefab to use for the authorized player (if this is null it uses the default NetworkManager defined player prefab)

    But how I can obtain the hash from the prefab? I have tried the GetHashCode from the GameObject, the global object id Hash shown in the NetworkObject component but I am unable to get a hash code that is working.

    I also tried the override option in the NetworkPrefabs and using an specific hash but not working either

    How I can access it?

    Using Version 1.1.0
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    677
    Are you using this for ConnectionApproval? I can't see a way of getting the GlobalObjectId at runtime, you can copy it from the prefab in the Unity inspector and paste it into the code.
     
  3. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    218
    Yes, I am using this for ConnectionApproval, It surprises me that I can't get this at runtime. Hardcoding these things is not ideal since a designer just wants to drag and drop the prefabs ( instead of seeing the id). I have checked and it is an internal value so with a getter we should be fine :)

    However, I have tried copying the GlobalObjectId in the Unity inspector ( from the network object component) and still It doesn't find it
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    677
    Is the prefab in the network manager's prefab list? I've tried it with a few different prefabs and in connection approval they were all spawned as the player.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,653
    You don't need the prefab hash, you need a reference to the prefab!

    If you want to provide on connection approval which prefab that client should be spawning as player object, make an indexed list of these prefabs (ie in a scriptable object or just any script on the NetworkManager object) and pass in the index of that prefab the client wants to use, so the server knows "that client wants to spawn prefab #3 in the list".

    You can even use the NetworkManager's network prefabs list if you feel that suffices. Just be sure you keep them in the right order, ie player prefabs first and then all the others.
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    677
    You can but you're moving away from it being a connection approval routine to just being a convenient way of passing data from client to server on connection. Not that I have any problems with that as I use it that way myself. What you are losing is the automatic spawning of the player prefab so that also has to be taken care of elsewhere.
     
    ledbetterMRI likes this.