k/GameObjectExtension.cs
namespace Sandbox.k;

public static class GameObjectExtension
{
	public static bool TryGetComponent<T>( this GameObject gameObject, out T component, bool logError = true )
		where T : Component
	{
		component = gameObject.GetComponent<T>();
		var hasComponent = component != null;
		if ( !hasComponent && logError )
		{
			Log.Error( $"{typeof(T)} is missing on GameObject {gameObject.Name}!" );
		}

		return hasComponent;
	}

	public static bool HasTag( this GameObject gameObject, string tag, bool includeAncestors = true )
	{
		return gameObject.Tags.Has( tag, includeAncestors );
	}

	public static int ChildCount( this GameObject gameObject )
	{
		return gameObject.Children.Count;
	}

	public static bool IsNull( this GameObject gameObject )
	{
		return gameObject == null || !gameObject.IsValid;
	}
}