Skip to content

Commit 83343a3

Browse files
fix: Editor NetworkBehaviour dependency check for NetworkObject not using root parent transform [MTT-2973] (#1841)
* fix Checking for NetworkObject from the root transform down. A child GameObject can still hold a NetworkBehaviour component if the parent of the child has a NetworkObject. * fix - editor Fixing issue with root transform when no GameObject exists. * fix Spawn manager not removing entries from ownership table * fix Updated the description of the fix for MTT-2973 * Update CHANGELOG.md MTT-2974 * fix Removing the ownership check in InvokeBehaviourOnLostOwnership because clients still need to update their table when the owner has changed to remove their ownership entry in their local ownership table. * test updating the integration test to check and make sure that the OwnershipToObjectsTable is having entries removed when ownership is lost. * update removing some debug code * update adding PR number * Update CHANGELOG.md
1 parent d563072 commit 83343a3

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2121
- Removed `com.unity.modules.animation`, `com.unity.modules.physics` and `com.unity.modules.physics2d` dependencies from the package (#1812)
2222

2323
### Fixed
24+
- Fixed NetworkBehaviour dependency verification check for an existing NetworkObject not searching from root parent transform relative GameObject. (#1841)
2425
- Fixed issue where entries were not being removed from the NetworkSpawnManager.OwnershipToObjectsTable. (#1838)
2526
- Fixed clarity for NetworkSceneManager client side notification when it receives a scene hash value that does not exist in its local hash table. (#1828)
2627
- Fixed client throws a key not found exception when it times out using UNet or UTP. (#1821)

com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ private void OnEnable()
225225

226226
internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist";
227227

228+
public static Transform GetRootParentTransform(Transform transform)
229+
{
230+
if (transform.parent == null || transform.parent == transform)
231+
{
232+
return transform;
233+
}
234+
return GetRootParentTransform(transform.parent);
235+
}
236+
228237
/// <summary>
229238
/// Used to determine if a GameObject has one or more NetworkBehaviours but
230239
/// does not already have a NetworkObject component. If not it will notify
@@ -233,17 +242,20 @@ private void OnEnable()
233242
public static void CheckForNetworkObject(GameObject gameObject, bool networkObjectRemoved = false)
234243
{
235244
// If there are no NetworkBehaviours or no gameObject, then exit early
236-
if (gameObject == null || gameObject.GetComponent<NetworkBehaviour>() == null && gameObject.GetComponentInChildren<NetworkBehaviour>() == null)
245+
if (gameObject == null || (gameObject.GetComponent<NetworkBehaviour>() == null && gameObject.GetComponentInChildren<NetworkBehaviour>() == null))
237246
{
238247
return;
239248
}
240249

241-
// Otherwise, check to see if there is a NetworkObject and if not notify the user that NetworkBehaviours
242-
// require that the relative GameObject has a NetworkObject component.
243-
var networkObject = gameObject.GetComponent<NetworkObject>();
250+
// Now get the root parent transform to the current GameObject (or itself)
251+
var rootTransform = GetRootParentTransform(gameObject.transform);
252+
253+
// Otherwise, check to see if there is any NetworkObject from the root GameObject down to all children.
254+
// If not, notify the user that NetworkBehaviours require that the relative GameObject has a NetworkObject component.
255+
var networkObject = rootTransform.GetComponent<NetworkObject>();
244256
if (networkObject == null)
245257
{
246-
networkObject = gameObject.GetComponentInChildren<NetworkObject>();
258+
networkObject = rootTransform.GetComponentInChildren<NetworkObject>();
247259

248260
if (networkObject == null)
249261
{

0 commit comments

Comments
 (0)