From 70c34ecf5d5dfb94191d4709eb3979eb584f8e5a Mon Sep 17 00:00:00 2001 From: mischa <16416509+vis2k@users.noreply.github.com> Date: Tue, 18 Jul 2023 15:37:04 +0800 Subject: [PATCH] NetworkBehaviour.OnValidate: GetComponentInParent(bool) 2020 support (credits FakeByte) --- Assets/Mirror/Core/NetworkBehaviour.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Assets/Mirror/Core/NetworkBehaviour.cs b/Assets/Mirror/Core/NetworkBehaviour.cs index 7dd5e7974..837ee017c 100644 --- a/Assets/Mirror/Core/NetworkBehaviour.cs +++ b/Assets/Mirror/Core/NetworkBehaviour.cs @@ -301,14 +301,23 @@ protected virtual void OnValidate() // parents. // only run this in Editor. don't add more runtime overhead. - // GetComponentInParent(includeInactive) is needed and only available in 2021+ - // Prefabs are not considered active, so this check requires to scan inactive. -#if UNITY_EDITOR && UNITY_2021_3_OR_NEWER + // GetComponentInParent(includeInactive) is needed because Prefabs are not + // considered active, so this check requires to scan inactive. +#if UNITY_EDITOR +#if UNITY_2021_3_OR_NEWER // 2021 has GetComponentInParents(active) if (GetComponent() == null && GetComponentInParent(true) == null) { Debug.LogError($"{GetType()} on {name} requires a NetworkIdentity. Please add a NetworkIdentity component to {name} or it's parents."); } +#elif UNITY_2020_3_OR_NEWER // 2020 only has GetComponentsInParents(active), we can use this too + NetworkIdentity[] parentsIds = GetComponentsInParent(true); + int parentIdsCount = parentsIds != null ? parentsIds.Length : 0; + if (GetComponent() == null && parentIdsCount == 0) + { + Debug.LogError($"{GetType()} on {name} requires a NetworkIdentity. Please add a NetworkIdentity component to {name} or it's parents."); + } +#endif #endif }