From 966af93e50abcd23d1aae28f3e11467964536308 Mon Sep 17 00:00:00 2001 From: vis2k Date: Fri, 13 Aug 2021 11:44:09 +0800 Subject: [PATCH] feat: Prevent Nested Network Managers (see #2867) --- Assets/Mirror/Runtime/NetworkManager.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index bcda61a9d..56e1815b4 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -141,6 +141,20 @@ public class NetworkManager : MonoBehaviour // virtual so that inheriting classes' OnValidate() can call base.OnValidate() too public virtual void OnValidate() { + // make sure someone doesn't accidentally add another NetworkManager + // need transform.root because when adding to a child, the parent's + // OnValidate isn't called. + foreach (NetworkManager manager in transform.root.GetComponentsInChildren()) + { + if (manager != this) + { + Debug.LogError($"{name} detected another component of type {typeof(NetworkManager)} in its hierarchy on {manager.name}. There can only be one, please remove one of them."); + // return early so that transport component isn't auto-added + // to the duplicate NetworkManager. + return; + } + } + // add transport if there is none yet. makes upgrading easier. if (transport == null) {