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;
}
}
}