Player won't respawn after timer hits zero.

I have a timer that counts down from 5 and the player will not respawn. I have the Die function caused from an explosion. Everything seems right, I just don’t understand.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Barrier : MonoBehaviour
{
    public Transform[] spawnPoints;
    public GameObject player;

    public GameObject[] targets;

    public bool dead;

    private float currentTime = 0f;
    private float startingTime = 5f;

    void Start()
    {
        dead = false;
        Spawn();

        currentTime = startingTime;
    }

    void Update()
    {
        print(currentTime);

        if (dead == true)
            currentTime -= 1 * Time.deltaTime;

        currentTime = Mathf.Clamp(currentTime, 0, 5);
    }

    public void Spawn()
    {
        if (dead == true)
        {
            for (int i = 0; i < spawnPoints.Length; i++)
            {
                List<Transform> freeSpawnPoints = new List<Transform>(spawnPoints);

                if (freeSpawnPoints.Count <= 0)
                    return;

                int index = Random.Range(0, freeSpawnPoints.Count);
                Transform pos = freeSpawnPoints[index];

                freeSpawnPoints.RemoveAt(index);

                player.SetActive(true);
                dead = false;

                player.transform.position = spawnPoints[index].position;
            }
        }
        else
        {
            for (int i = 0; i < spawnPoints.Length; i++)
            {
                List<Transform> freeSpawnPoints = new List<Transform>(spawnPoints);

                if (freeSpawnPoints.Count <= 0)
                    return;

                int index = Random.Range(0, freeSpawnPoints.Count);
                Transform pos = freeSpawnPoints[index];

                freeSpawnPoints.RemoveAt(index);

                player.SetActive(true);
                dead = false;

                player.transform.position = spawnPoints[index].position;
            }
        }
    }

    private void OnTriggerEnter(Collider collision)
    {
        if (collision.transform.CompareTag("Player"))
        {
            for (int i = 0; i < spawnPoints.Length; i++)
            {
                List<Transform> freeSpawnPoints = new List<Transform>(spawnPoints);

                if (freeSpawnPoints.Count <= 0)
                    return;

                int index = Random.Range(0, freeSpawnPoints.Count);
                Transform pos = freeSpawnPoints[index];

                freeSpawnPoints.RemoveAt(index);

                collision.transform.position = spawnPoints[index].position;
            }
        }

        if (collision.transform.CompareTag("Enemy"))
            for (int i = 0; i < targets.Length; i++)
                Destroy(targets*);*

}

public void Die()
{
dead = true;

player.SetActive(false);
print(“Player died.”);

if (currentTime <= 0f)
{
Spawn();

currentTime = startingTime;
startingTime = 5f;
}
}
}

You are checking if currentTime is exactly 0 in the Die() function… Since it is most likely that it will be less than zero you should check for that.

if (currentTime <= 0f)

If Die() is being called by the death-causing explosion, then unless that explosion script remains in the scene and is running that function in its Update() or else keeps triggering from collision somehow, it must only being calling Die() a single time, detecting that the currentTime is not zero and thus not calling the Spawn function.

I don’t recommend running Die() in either of those manners I described as it would be heinous; instead, since you have a set amount of time you want to pass before the player respawns I recommend having the explosion call a coroutine. like this:

public IEnumerator Die()
         {
            dead = true;
            playerSetActive(false);
            print("Player died.");
    
            yield return new WaitForSeconds(startingTime); // or just put 5 here. you wont need your countdown from Update() if you use a coroutine for this.
            Spawn();

        }

and then in your explosion script you can call that by putting

StartCoroutine(objectYourScripisAttachedToHere.GetComponent<Barrier>().Die());

… that should work, I think, I usually just have my coroutines in the same script as they are called so I’m not too sure about how to call them from another exactly but this thread should be able to help you with that. You could also just have the explosion send a death bool to this script and start the coroutine in that case.