- Home /
Forced player reset not being delayed
Hi, Not particularly wised up on c# scripting,so getting a bit stumped.
I have a racer on a track, with a number of game objects set up as safe reset points. Manual reset works fine, as does forced reset when you leave the track (hit a trigger collider), however the player gets forced reset immediately upon leaving the track, rather than being delayed by the desired 2 seconds.
Can anyone point me in the right direction for getting the delay to work?
using UnityEngine;
using System.Collections;
public class ResetToTrack : MonoBehaviour
{
public Transform[] resetPoints;
void Update()
{
{
if (Input.GetButtonDown ("reset"))
{
Debug.Log("Manual Reset");
ResetBoard ();
}
}
}
IEnumerator OnTriggerExit (Collider trig)
{
if (trig.gameObject.tag == "Respawn")
yield return new WaitForSeconds(2);
Debug.Log("Forced Reset");
ResetBoard ();
}
void ResetBoard()
{
Transform closestTransform = null;
float closestDistance = 999999999;
Vector3 currentPos = transform.position;
foreach (Transform trans in resetPoints)
{
float currentDistance = Vector3.Distance (currentPos, trans.position);
if (currentDistance < closestDistance)
{
closestDistance = currentDistance;
closestTransform = trans;
}
}
transform.position = closestTransform.position;
transform.rotation = closestTransform.rotation;
Debug.Log("You Got Reset");
}
}
Answer by Maerig · May 21, 2014 at 05:28 AM
I think your collider isn't tagged with "Respawn", but since your code is like this
if (trig.gameObject.tag == "Respawn")
yield return new WaitForSeconds(2);
Debug.Log("Forced Reset");
ResetBoard ();
it only ignores the yield return new WaitForSeconds(2);
line.
Urgh, how did i miss that! Thanks, that seems to have fixed it :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613145500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
i need to delay an action but my code isnt working 1 Answer
How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers
Delay between each instantiation 3 Answers
trying to delay a loop using yield - not working 1 Answer
How to reload a gun with delay ? 2 Answers