diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index 0bbc5990..9ed77826 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -10,11 +10,13 @@ namespace NewHorizons.Utility public static class SearchUtilities { private static readonly Dictionary CachedGameObjects = new Dictionary(); + private static readonly Dictionary CachedRootGameObjects = new Dictionary(); public static void ClearCache() { NHLogger.LogVerbose("Clearing search cache"); CachedGameObjects.Clear(); + CachedRootGameObjects.Clear(); } public static List FindObjectsOfTypeAndName(string name) where T : Object @@ -107,8 +109,16 @@ namespace NewHorizons.Utility // 2: find inactive using root + transform.find var names = path.Split('/'); + // Cache the root objects so we don't loop through all of them each time var rootName = names[0]; - var root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == rootName); + if (!CachedRootGameObjects.TryGetValue(rootName, out var root)) + { + root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == rootName); + if (root != null) + { + CachedRootGameObjects.Add(rootName, root); + } + } var childPath = string.Join("/", names.Skip(1)); go = root ? root.FindChild(childPath) : null;