ho stanziato due nemici, il primo viene creato e funziona correttamente, dopo la sua morte dovrebbe essere creato un altro nemico, però ciò non accade:"using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[System.Serializable]
public class EnemySequence
{
public GameObject enemyPrefab; // Prefab of the enemy
public Transform spawnPoint; // Spawn point for the enemy
public GameObject[] imagesOnEnemyReach; // Images to fade in when the enemy reaches the target point
public GameObject[] imagesAfterEnemyDeath; // Images to fade in after the enemy is defeated
public Transform targetPoint; // The designated point the enemy should move to
public float stopDistance = 0f; // The stopping distance when the enemy reaches the target point
}
[System.Serializable]
public class SequenceStep
{
public enum StepType { FadeInAndOutImage, SpawnEnemy }
public StepType stepType;

public GameObject image;                      // For FadeInAndOutImage
public EnemySequence enemySequence;           // For SpawnEnemy
}
public class dissolvenzaImmagini : MonoBehaviour
{
[Header("Sequence Settings")]

public SequenceStep[] sequenceSteps;            // Array of sequence steps to process in order

[Header("Fade Settings")]
public float fadeDuration = 3f;                 // Duration of the fade
public float displayDuration = 2f;              // Duration to display the images

[Header("Player and Enemy Settings")]
public controller playerMovement;               // Reference to player movement script
public Player_combat_Mattina combat;            // Reference to player combat script
public enemySpawner[] enemySpawners;            // References to enemy spawners

private GameObject currentEnemy;                // Reference to the current enemy

void Start()
{
    // Initialize images in each sequence step
    foreach (var step in sequenceSteps)
    {
        if (step.stepType == SequenceStep.StepType.FadeInAndOutImage && step.image != null)
        {
            SetImageAlpha(step.image, 0f);
        }
        else if (step.stepType == SequenceStep.StepType.SpawnEnemy && step.enemySequence != null)
        {
            // Initialize imagesOnEnemyReach
            if (step.enemySequence.imagesOnEnemyReach != null)
            {
                foreach (var imgObj in step.enemySequence.imagesOnEnemyReach)
                {
                    SetImageAlpha(imgObj, 0f);
                }
            }

            // Initialize imagesAfterEnemyDeath
            if (step.enemySequence.imagesAfterEnemyDeath != null)
            {
                foreach (var imgObj in step.enemySequence.imagesAfterEnemyDeath)
                {
                    SetImageAlpha(imgObj, 0f);
                }
            }
        }
    }

    DisablePlayerAndEnemies();
    StartCoroutine(RunSequences());
}

void DisablePlayerAndEnemies()
{
    if (playerMovement != null) playerMovement.enabled = false;
    if (combat != null) combat.enabled = false;

    if (enemySpawners != null)
    {
        foreach (enemySpawner spawner in enemySpawners)
        {
            if (spawner != null) spawner.enabled = false;
        }
    }
}

void EnablePlayerAndEnemies()
{
    if (playerMovement != null) playerMovement.enabled = true;
    if (combat != null) combat.enabled = true;

    if (enemySpawners != null)
    {
        foreach (enemySpawner spawner in enemySpawners)
        {
            if (spawner != null) spawner.enabled = true;
        }
    }
}

IEnumerator RunSequences()
{
    foreach (var step in sequenceSteps)
    {
        switch (step.stepType)
        {
            case SequenceStep.StepType.FadeInAndOutImage:
                if (step.image != null)
                {
                    yield return StartCoroutine(FadeInImage(step.image));
                    yield return new WaitForSeconds(displayDuration);
                    yield return StartCoroutine(FadeOutImage(step.image));
                }
                break;
            case SequenceStep.StepType.SpawnEnemy:
                if (step.enemySequence != null)
                {
                    yield return StartCoroutine(HandleEnemySequence(step.enemySequence));
                }
                break;
        }
    }
    // Re-enable player and enemies
    EnablePlayerAndEnemies();
}

IEnumerator HandleEnemySequence(EnemySequence sequence)
{
    // Spawn the enemy
    currentEnemy = Instantiate(
        sequence.enemyPrefab,
        sequence.spawnPoint.position,
        Quaternion.identity);

    // Set up the enemy's movement and target
    EnemyMovement movement = currentEnemy.GetComponent<EnemyMovement>();
    if (movement == null)
    {
        movement = currentEnemy.AddComponent<EnemyMovement>();
    }
    movement.target = sequence.targetPoint != null ? sequence.targetPoint : transform; // Use sequence target or default to this object's transform
    movement.stopDistance = sequence.stopDistance;

    // Wait until the enemy reaches the target
    bool enemyReachedTarget = false;
    movement.OnReachTarget += () =>
    {
        enemyReachedTarget = true;
    };

    while (!enemyReachedTarget)
    {
        yield return null;
    }

    // Display images when the enemy reaches the target
    if (sequence.imagesOnEnemyReach != null && sequence.imagesOnEnemyReach.Length > 0)
    {
        foreach (var img in sequence.imagesOnEnemyReach)
        {
            if (img != null)
            {
                yield return StartCoroutine(FadeInImage(img));
                yield return new WaitForSeconds(displayDuration);
                yield return StartCoroutine(FadeOutImage(img));
            }
        }
    }

    // Wait until the enemy is destroyed
    Health enemyHealth = currentEnemy.GetComponent<Health>();
    while (enemyHealth != null && enemyHealth.IsAlive())
    {
        yield return null;
    }

    // Display images after the enemy is defeated
    if (sequence.imagesAfterEnemyDeath != null && sequence.imagesAfterEnemyDeath.Length > 0)
    {
        foreach (var img in sequence.imagesAfterEnemyDeath)
        {
            if (img != null)
            {
                yield return StartCoroutine(FadeInImage(img));
                yield return new WaitForSeconds(displayDuration);
                yield return StartCoroutine(FadeOutImage(img));
            }
        }
    }
}

IEnumerator FadeInImage(GameObject imageObj)
{
    float elapsedTime = 0f;
    Image[] images = imageObj.GetComponentsInChildren<Image>();
    while (elapsedTime < fadeDuration)
    {
        elapsedTime += Time.deltaTime;
        float alpha = Mathf.Clamp01(elapsedTime / fadeDuration);
        SetImagesAlpha(images, alpha);
        yield return null;
    }
    SetImagesAlpha(images, 1f);
}

IEnumerator FadeOutImage(GameObject imageObj)
{
    float elapsedTime = 0f;
    Image[] images = imageObj.GetComponentsInChildren<Image>();
    while (elapsedTime < fadeDuration)
    {
        elapsedTime += Time.deltaTime;
        float alpha = Mathf.Clamp01(1f - (elapsedTime / fadeDuration));
        SetImagesAlpha(images, alpha);
        yield return null;
    }
    SetImagesAlpha(images, 0f);
}

void SetImagesAlpha(Image[] images, float alpha)
{
    foreach (Image img in images)
    {
        if (img != null)
        {
            Color color = img.color;
            color.a = alpha;
            img.color = color;
        }
    }
}

void SetImageAlpha(GameObject imageObj, float alpha)
{
    Image[] images = imageObj.GetComponentsInChildren<Image>();
    SetImagesAlpha(images, alpha);
}
}","using UnityEngine;
using UnityEngine.AI;
using System;
public class EnemyMovement : MonoBehaviour
{
[Header("Target Settings")]
public Transform target; // Reference to the target Transform
public float stopDistance = 0f; // Distance at which the enemy stops moving towards the target

[Header("Animation Settings")]
public Animator animator;               // Reference to the Animator

[Header("Movement Settings")]
public float chaseRange = 100f;         // Set a large range or adjust as needed

public Action OnReachTarget;            // Event to signal when the enemy reaches the target

private NavMeshAgent agent;
private SpriteRenderer spriteRenderer;
private bool hasReachedTarget = false;  // Flag to ensure OnReachTarget is called only once

protected virtual void Start()
{
    // Get the NavMeshAgent component
    agent = GetComponent<NavMeshAgent>();
    if (agent == null)
    {
        Debug.LogError("NavMeshAgent missing on Enemy.");
        enabled = false;
        return;
    }

    // Set stopping distance
    agent.stoppingDistance = stopDistance;
    agent.autoBraking = true;

    // Get the SpriteRenderer component (optional)
    spriteRenderer = GetComponent<SpriteRenderer>();

    // Get the Animator component if not assigned
    if (animator == null)
    {
        animator = GetComponent<Animator>();
    }
}

protected virtual void Update()
{
    if (target == null) return;

    if (!hasReachedTarget)
    {
        agent.SetDestination(target.position);

        if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
        {
            hasReachedTarget = true;
            agent.isStopped = true;
            if (animator != null)
            {
                animator.SetBool("running", false);
            }
            OnReachTarget?.Invoke();
        }
        else
        {
            agent.isStopped = false;
            if (animator != null)
            {
                animator.SetBool("running", true);
            }
        }

        FlipSprite();
    }
}

protected void FlipSprite()
{
    if (spriteRenderer == null) return;

    Vector3 direction = agent.velocity;

    if (direction.x != 0)
    {
        spriteRenderer.flipX = direction.x < 0;
    }
}
}","using UnityEngine;
public class Health : MonoBehaviour
{
public int maxHealth = 100;
private int currentHealth;

void Start()
{
    currentHealth = maxHealth;
}

public bool IsAlive()
{
    return currentHealth > 0;
}

public void TakeDamage(int amount)
{
    currentHealth -= amount;
    if (currentHealth <= 0)
    {
        Die();
    }
}

void Die()
{
    // Potresti aggiungere qui effetti visivi o suoni
    Destroy(gameObject);
}
}"