fix(NetworkBehaviour): Improved OnValidate Error Logging

References the offending object so it gets highlighted in the Editor when the error is clicked.
This commit is contained in:
MrGadget1024 2024-01-09 19:43:17 -05:00
parent 88a9d7dcb1
commit 28b1e8b374

View File

@ -316,14 +316,14 @@ protected virtual void OnValidate()
if (GetComponent<NetworkIdentity>() == null && if (GetComponent<NetworkIdentity>() == null &&
GetComponentInParent<NetworkIdentity>(true) == null) GetComponentInParent<NetworkIdentity>(true) == null)
{ {
Debug.LogError($"{GetType()} on {name} requires a NetworkIdentity. Please add a NetworkIdentity component to {name} or it's parents."); Debug.LogError($"{GetType()} on {name} requires a NetworkIdentity. Please add a NetworkIdentity component to {name} or it's parents.", this);
} }
#elif UNITY_2020_3_OR_NEWER // 2020 only has GetComponentsInParents(active), we can use this too #elif UNITY_2020_3_OR_NEWER // 2020 only has GetComponentsInParents(active), we can use this too
NetworkIdentity[] parentsIds = GetComponentsInParent<NetworkIdentity>(true); NetworkIdentity[] parentsIds = GetComponentsInParent<NetworkIdentity>(true);
int parentIdsCount = parentsIds != null ? parentsIds.Length : 0; int parentIdsCount = parentsIds != null ? parentsIds.Length : 0;
if (GetComponent<NetworkIdentity>() == null && parentIdsCount == 0) if (GetComponent<NetworkIdentity>() == null && parentIdsCount == 0)
{ {
Debug.LogError($"{GetType()} on {name} requires a NetworkIdentity. Please add a NetworkIdentity component to {name} or it's parents."); Debug.LogError($"{GetType()} on {name} requires a NetworkIdentity. Please add a NetworkIdentity component to {name} or it's parents.", this);
} }
#endif #endif
#endif #endif