- Home /
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[i]);
}
public void Die()
{
dead = true;
player.SetActive(false);
print("Player died.");
if (currentTime <= 0f)
{
Spawn();
currentTime = startingTime;
startingTime = 5f;
}
}
}
Answer by rh_galaxy · Feb 15, 2020 at 06:35 PM
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)
i have tried that too, it doesn't work. i also added a mathf.clamp too.
Answer by utzsar · Feb 16, 2020 at 01:56 PM
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.
so i think i see the problem. everything you told me to do works, but i think its my Spawn() function. i put a Debug.Log("Spawned!"); in the Die() IEnumerator and it worked, but when it reached the Spawn() nothing happened.
i got rid of everything in the update that you told me to as well.
Oh, I assumed that part worked since you have it in Start() but I'm guessing you probably have the player object active when the game starts anyway which is probably why you wouldn't have noticed something was wrong with that function. Need any help sorting that out? $$anonymous$$ake sure you added your spawnPoint array objects in the editor, 'cause it doesn't look like you assign them anywhere in the script. If it's empty, then your freespawnpoints list will be empty, and the spawn function will be canceling out on that "return" after confir$$anonymous$$g the list has no spawnpoints in it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
make a countdown timer 1 Answer
Distribute terrain in zones 3 Answers
How do I make the timeScale not affect the timer? 1 Answer
Trying to make a timer work.. 0 Answers