using UnityEngine;

public class CharacterSpawner : MonoBehaviour
{
    [SerializeField] private GameObject characterPrefab;
    [SerializeField] private Transform startPosition;

    private void Start()
    {
        if (characterPrefab != null && startPosition != null)
        {
            GameObject spawnedCharacter = Instantiate(characterPrefab, startPosition.position, startPosition.rotation);

            // Get the PlayerHealth component and initialize it
            PlayerHealth playerHealth = spawnedCharacter.GetComponent<PlayerHealth>();
            if (playerHealth != null)
            {
                playerHealth.InitializeHealth();
            }
        }
        else
        {
            Debug.LogError("CharacterPrefab or StartPosition is not assigned in the inspector!");
        }
    }
}


----------

// Updated PlayerHealth.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerHealth : MonoBehaviour
{
    [SerializeField] private float maxHealth = 100f;
    // [SerializeField] private float currentHealth;
    private float currentHealth;
    [SerializeField] private Slider healthSlider;

    private void Awake()
    {
        // Initialize health
        InitializeHealth();
        currentHealth = maxHealth;
        Debug.Log($"Initialized Health: {currentHealth}");

        // Configure the slider
        if (healthSlider != null)
        {
            healthSlider.minValue = 0f;
            healthSlider.maxValue = 1f;
            UpdateHealthUI();
        }
    }

    public void InitializeHealth()
    {
        // Initialize health
        currentHealth = maxHealth;
        Debug.Log($"Initialized Health: {currentHealth}");

        // Configure the slider
        if (healthSlider != null)
        {
            healthSlider.minValue = 0f;
            healthSlider.maxValue = 1f;
            UpdateHealthUI();
        }
    }

    public void SetHealth(float health)
    {
        currentHealth = Mathf.Clamp(health, 0f, maxHealth);
        Debug.Log($"SetHealth called. Input Health: {health}, Clamped Health: {currentHealth}, Max Health: {maxHealth}");
        UpdateHealthUI();
    }

    public void TakeDamage(float damage)
    {
        if (currentHealth <= 0)
        {
            Debug.Log("Health already zero, ignoring damage.");
            return;
        }

        Debug.Log($"Before damage: Health = {currentHealth}, Damage = {damage}");
        currentHealth = Mathf.Max(0f, currentHealth - damage);
        Debug.Log($"After damage: Health = {currentHealth}");
        UpdateHealthUI();
    }

    public void Heal(float amount)
    {
        currentHealth = Mathf.Min(maxHealth, currentHealth + amount);
        UpdateHealthUI();
    }

    private void UpdateHealthUI()
    {
        if (healthSlider != null)
        {
            // Convert current health to 0-1 range for the slider
            healthSlider.value = currentHealth / maxHealth;
        }

        Debug.Log($"Current Health: {currentHealth} (Slider Value: {(healthSlider != null ? healthSlider.value.ToString() : "No Slider")})");
    }

    public float GetCurrentHealth() => currentHealth;
    public float GetMaxHealth() => maxHealth;
}
---------

using UnityEngine;
using Assets.FantasyMonsters.Common.Scripts;
using System.Collections;

public class EnemyMovement : MonoBehaviour
{
    [Header("Movement Settings")]
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private float baseStopDistance = 5f;
    [SerializeField] private float stopDistanceVariation = 1.5f;
    [SerializeField] private bool faceMovementDirection = true;

    [Header("Attack Settings")]
    [SerializeField] private float attackCooldown = 2f;
    [SerializeField] private float normalAttackDamage = 10f;
    [SerializeField] private float altAttackDamage = 20f;

    [Header("References")]
    [SerializeField] private Transform player;
    [SerializeField] private bool debugMode = true;

    private float actualStopDistance;
    private Monster monsterScript;
    private Animator animator;
    private PlayerHealth playerHealth;
    private bool canAttack = true;

    private void Start()
    {
        // Initialize stop distance with random variation
        actualStopDistance = baseStopDistance + Random.Range(-stopDistanceVariation, stopDistanceVariation);

        // Get required components
        monsterScript = GetComponent<Monster>();
        animator = GetComponent<Animator>();

        // Set up player reference and health component
        if (player != null)
        {
            playerHealth = player.GetComponent<PlayerHealth>();
            if (playerHealth == null && debugMode)
            {
                Debug.LogError("PlayerHealth component not found on player!");
            }
        }

        // Check for required components
        if (monsterScript == null && debugMode)
        {
            Debug.LogError("Monster component not found on enemy!");
        }
        if (animator == null && debugMode)
        {
            Debug.LogError("Animator component not found on enemy!");
        }
    }

    private void Update()
    {
        if (player == null) return;

        float distanceToPlayer = Vector3.Distance(transform.position, player.position);

        if (distanceToPlayer > actualStopDistance)
        {
            // Move towards player
            transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);

            // Handle rotation
            if (faceMovementDirection)
            {
                transform.rotation = Quaternion.Euler(0, -90, 0);
            }

            // Update animation state
            monsterScript?.SetState(MonsterState.Walk);

            // Reset attack animation
            if (animator != null)
            {
                animator.ResetTrigger("Attack");
            }
        }
        else
        {
            // When in range, stop and attack
            monsterScript?.SetState(MonsterState.Idle);

            // Only try to attack if we can
            if (canAttack && playerHealth != null)
            {
                StartCoroutine(Attack());
            }
        }

        if (debugMode)
        {
            Debug.DrawLine(transform.position, transform.position + Vector3.left * actualStopDistance, Color.red);
        }
    }

    private IEnumerator Attack()
    {
        if (!canAttack) yield break;

        canAttack = false;

        if (debugMode)
        {
            Debug.Log($"[{gameObject.name}] Starting attack");
        }

        // Play attack animation
        if (animator != null)
        {
            animator.SetTrigger("Attack");
        }

        // Deal damage
        if (playerHealth != null)
        {
            if (debugMode)
            {
                Debug.Log($"[{gameObject.name}] Dealing {normalAttackDamage} damage");
            }
            playerHealth.TakeDamage(normalAttackDamage);
        }

        // Wait for cooldown
        yield return new WaitForSeconds(attackCooldown);

        canAttack = true;
    }

    public void SetPlayer(Transform playerTransform)
    {
        player = playerTransform;
        if (player != null)
        {
            playerHealth = player.GetComponent<PlayerHealth>();
        }
    }

    private void OnDisable()
    {
        // Make sure we can attack when re-enabled
        canAttack = true;
    }
}