SortChildrenNetworkObjects Bug
Description
First of all, I’m not a game developer — I just encountered what seems to be a Unity-related issue while playing a certain game.
Let me briefly describe the problem.
This game was upgraded from Unity 2022.3.9 to 2022.3.62f2 to address a Unity vulnerability.
In 2022.3.62f2, the NetworkObject class introduced a new field called m_CachedParent, and that’s where the issue began.
In this game (Lethal Company), there’s a NetworkObject whose default transform.parent is an object in the map scene. When players leave that map, the entire map gets destroyed. The NetworkObject is then brought back to the ship, so it still exists — but its m_CachedParent field becomes a Destroyed state.
When a new player joins the game, SceneEventData.SortChildrenNetworkObjects tries to sort these objects, which leads to an exception.
However, I noticed that SortChildrenNetworkObjects includes a != null check — yet that check doesn’t seem to work properly.
I tried modifying the function, and interestingly, if I simply add a Debug.Log statement inside it, the != null check starts working as expected.
I found this behavior very strange, so I decided to report it.
Reproduce Steps(Use LethalCompany Game)
-
Install any late join mod (e.g., LobbyControl).
-
Start the game, go into the facility, remove the LungApparatus, and bring it back to the ship.
-
Launch the ship.
-
When another player joins the lobby,will an error occurs.
Screenshots
Environment
- OS: Windows
- Unity Version: 2022.3.62f2
- Netcode Version: [e.g. 1.0.0-pre.6]
- Netcode Commit: [e.g. https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/commit/ba418fa5b600ad9eb61fab0575f12fbecc2c6520]
- Netcode Topology: [e.g. Client-Server, Distributed Authority, etc.]
Additional Context
Add any other context about the problem here. Logs, code snippets would be useful here but please also consider attaching a minimal Unity project that reproduces the issue.
I believe this is an important reason for the bug: the game never uses the m_CachedParent field from start to finish. The assignment and usage of m_CachedParent are entirely handled by Unity.
@chuxiaaaa
I see a few places where this could happen. More specific to your scenario where SceneEvenData.SortChildrenNetworkObjects invokes this:
var firstParent = first.GetCachedParent()?.GetComponent<NetworkObject>();
Which m_CachedParent is a Transform and if the GameObject is no longer valid you would get that exception.
However, as you pointed out there really isn't any place where this is "reset" if the NetworkObject persists (after being despawned) and then is re-used in a new session. We should set m_CachedParent locally at the beginning of the spawn process to whatever the current parent is for the instance.
I am importing this issue to be resolved. 👍
Just so we can keep track, which version of NGO are you using?
@NoelStephensUnity
Sorry, I haven't found a way to check the NGO version, but I only know it's Unity 2022.3.62f2. I'll upload the Unity.Netcode.Runtime.dllfile to see if it helps you. By the way, I might also need to mention that for this issue, after I modified the SortChildrenNetworkObjects method using dnSpy, just adding a Debug.Log at the very beginning prevented the issue from occurring again. Like this:
private int SortChildrenNetworkObjects(NetworkObject first, NetworkObject second)
{
Debug.Log("aaa");
Transform cachedParent = first.GetCachedParent();
NetworkObject networkObject = ((cachedParent != null) ? cachedParent.GetComponent<NetworkObject>() : null);
if (networkObject != null && networkObject == second)
{
return 1;
}
Transform cachedParent2 = second.GetCachedParent();
NetworkObject networkObject2 = ((cachedParent2 != null) ? cachedParent2.GetComponent<NetworkObject>() : null);
if (networkObject2 != null && networkObject2 == first)
{
return -1;
}
return 0;
}
@chuxiaaaa You can find the version of Netcode for GameObjects by opening the package manager and selecting the installed Netcode for GameObjects package:
@NoelStephensUnity I think it's version 1.12.0 because the modding community for this game all uses this version of Netcode for GameObjects to create mods.
I think it's version 1.12.0 because the modding community for this game all uses this version of Netcode for GameObjects to create mods.
If that is the case, then the developer for the game needs to update to a more current version of NGO in order to resolve that issue (i.e. even if we were to fix the issue in say a hot-patch fix for v1.12.1, even if the issue is already fixed in the more current v2.7.0, the game would still be using 1.12.0 and would still need to be updated with the v1.12.1 hot-patch fix)
Before we allocate an engineer to the task, could you find out if this is the case? If so, then you will need to get the developer of the game involved in this fix.
@NoelStephensUnity I understand, but the game developers may not necessarily address this issue. I have also created a mod to fix this problem (mod). I just found this issue quite unusual, so I reported it to see if you consider it necessary to fix.