using UnityEngine;

public class UltimateParentFinder : MonoBehaviour
{
    public GameObject GetUltimateParent(GameObject child)
    {
        Transform currentTransform = child.transform;
        Transform ultimateParent = null;

        while (currentTransform != null)
        {
            if (currentTransform.parent == null)
            {
                ultimateParent = currentTransform;
                break;
            }
            currentTransform = currentTransform.parent;
        }
        
        if (ultimateParent != null && CharacterPrefabManager.Instance.IsValidCharacter(ultimateParent.gameObject))
        {
            return ultimateParent.gameObject;
        }
        else
        {
            return null;
        }
    }
}