using UnityEngine; namespace gay.lilyy.Common { public static class ComponentHelper { /// /// Searches for a component in children of the root GameObject. /// Returns null if no component is found. /// Logs an error and returns null if multiple components are found. /// /// The component type to search for /// The root GameObject to search in /// The component if exactly one is found, null otherwise public static T? GetComponentInChildrenWithError(GameObject root) where T : Component { if (root == null) return null; T[] components = root.GetComponentsInChildren(true); if (components.Length == 0) { return null; } if (components.Length > 1) { Debug.LogError($"Multiple {typeof(T).Name} components found in children of {root.name}. Only one is allowed."); return null; } return components[0]; } /// /// Searches for a component in children of the root Transform. /// Returns null if no component is found. /// Logs an error and returns null if multiple components are found. /// /// The component type to search for /// The root Transform to search in /// The component if exactly one is found, null otherwise public static T? GetComponentInChildrenWithError(Transform root) where T : Component { if (root == null) return null; T[] components = root.GetComponentsInChildren(true); if (components.Length == 0) { return null; } if (components.Length > 1) { Debug.LogError($"Multiple {typeof(T).Name} components found in children of {root.name}. Only one is allowed."); return null; } return components[0]; } } }