NetworkObject.GlobalObjectIdHash serializes invalid values when in Prefab Mode
See original GitHub issueDescription
NetworkObject.GlobalObjectIdHash
serializes to a non-unique invalid value if it is saved when the object that holds it is in Prefab Mode.
Specifically, the issue originates from var globalObjectIdString = UnityEditor.GlobalObjectId.GetGlobalObjectIdSlow(this).ToString();
, in NetworkObject.GenerateGlobalObjectIdHash
which will always return GlobalObjectId_V1-0-00000000000000000000000000000000-0-0
in prefab mode.
Once the object has a corrupt hash, it seems impossible to fix, and the prefab is doomed to stay corrupt without manual script tampering.
Reproduce Steps
- In the project view window, right click -> create -> ‘Prefab’
- Double click the prefab to enter ‘Prefab Mode’ in which you can edit the newly made prefab
- Add a ‘NetworkObject’ component and witness its readonly GlobalObjectIdHash to be
951099334
which is the hash ofGlobalObjectId_V1-0-00000000000000000000000000000000-0-0
. - Ctrl+S to save the prefab
- Inspect the newly made
New Prefab.prefab
file in any text editor of your choice, and witness thatGlobalObjectIdHash: 951099334
- (bonus), exit prefab mode, and select
New Prefab
in the project view, witness that theGlobalObjectIdHash
is a non-951099334 value, despite being serialized as such.
Actual Outcome
GlobalObjectIdHash is broken and non-unique, unusable.
Expected Outcome
GlobalObjectIdHash should always be unique traceable to a unique object between different clients.
Screenshots
If applicable, add screenshots to help explain your problem.
Environment
- OS: Windows 10
- Unity Version: 2023.1.3f1
- Netcode Version: 1.5.1
Issue Analytics
- State:
- Created 2 months ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
@Yoraiz0r Ok… finally finished figuring out what that last chunk of code was being used for and made a slight adjustment to the “type” from an imported asset (type 1) to a source asset (type 3). When editing a prefab the GlobalObjectId.GetGlobalObjectIdSlow views it as a scene object with the associated file identifier as being all 0’s since the scene used to edit the prefab is temporary. Going to run a few more tests to compare the values in-editor and in a runtime build… but I am pretty sure it looks like we want it to generate the identifier as a source asset (the prefab asset itself).
Close to putting up a PR. 👍
@Yoraiz0r & @slb1988 Thank you both for your detailed response and time spent investigating this issue. I am going to be testing both of your suggested approaches (possibly end up with a combination of both), and will post back here with the results. This is an area that has needed attention indeed, and again…wanted to thank you both for your time spent on this. (very promising suggested fixes) 🥇 👍