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

Resolved What is the best way to handle NetworkObjects in different Unity Projects

Discussion in 'Netcode for GameObjects' started by invisage, Oct 9, 2023.

  1. invisage

    invisage

    Joined:
    Jun 27, 2019
    Posts:
    23
    I am in the middle of porting over an existing project to use Netcode. I have one project that is setup for iPad, the other with VR.

    Issue I am having is with using NetworkObjects and miss matched GlobalObjectIdHash. I have tried:

    * Unchecking Force Same Prefabs in Network Manger.
    * Overriding the Hash in the NetworkPrefabList scriptable object (this seems to do nothing?)

    What are some other options I can look at to solve this problem (without combining both into one project which would be a nightmare).

    Thanks.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,615
    I‘m thinking, perhaps the global Id persists between the project if you use the exact same asset. Try creating a network prefab in one project and make it work with NetworkManager. Then export this prefab as .unitypackage and import it in another project to see if it still complains about GlobalHashId mismatch.

    Btw pretty sure this global id hash has nothing to do with the object‘s GetHashCode() method if that‘s what you implemented.
     
  3. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    676
    I was curious about this so I had a look at the source code. You can override what network object to spawn on a client with something like this:
    Code (CSharp):
    1. public class SceneController : MonoBehaviour
    2. {
    3.     [SerializeField] Card card;
    4.  
    5.     private void Start()
    6.     {
    7.         Application.targetFrameRate = 15;
    8.  
    9.         NetworkManager.Singleton.OnClientStarted += OnClientStarted;
    10.         NetworkManager.Singleton.StartClient();
    11.     }
    12.  
    13.     private void OnClientStarted()
    14.     {
    15.         NetworkPrefab networkPrefab = new NetworkPrefab();
    16.         networkPrefab.Override = NetworkPrefabOverride.Hash;
    17.         networkPrefab.OverridingTargetPrefab = card.gameObject;
    18.  
    19.         NetworkManager.Singleton.NetworkConfig.Prefabs.NetworkPrefabOverrideLinks.Add(2893958005, networkPrefab);
    20.     }
    21. }
    It doesn't look like the server provides the GlobalObjectIdHash's so they'll have to be hard-coded in some way like the above. In quick testing network variables and rpc's on the object appeared to behave normally. I tested this without a network prefab list as it seems to be redundant with this method.