- Home /
How can i restart a timer?
So I'm trying to make a timed pressure pad that can either go for 3 seconds or 5 seconds, depending on the configuration, and this all works, but it doesn't reactivate if I step on it after it ended the first time, and I can't figure out where I went wrong, if you could help, that's be great!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimedPressurePad : MonoBehaviour
{
public float PadTime = 5f;
public Material pressed;
public Material unpressed;
public GameObject Object;
public Material fivePad;
public Material fourPad;
public Material threePad;
public Material twoPad;
public Material onePad;
private void OnTriggerEnter(Collider other)
{
if (PadTime == 5f)
Object.GetComponent<Renderer>().material = fivePad;
else if (PadTime == 3f)
Object.GetComponent<Renderer>().material = threePad;
StartCoroutine(PadTimer());
}
IEnumerator PadTimer()
{
while (PadTime !=0f)
{
yield return new WaitForSeconds(PadTime / PadTime);
PadTime = PadTime - 1f;
Debug.Log(PadTime);
if (PadTime == 5f)
Object.GetComponent<Renderer>().material = fivePad;
else if (PadTime == 4f)
Object.GetComponent<Renderer>().material = fourPad;
else if (PadTime == 3f)
Object.GetComponent<Renderer>().material = threePad;
else if (PadTime == 2f)
Object.GetComponent<Renderer>().material = twoPad;
else if (PadTime == 1f)
Object.GetComponent<Renderer>().material = onePad;
}
Object.GetComponent<Renderer>().material = unpressed;
StopCoroutine(PadTimer());
}
}
Try to rename Object in something else :
public GameObject myObject;
Object is a key word (it is not colorized like the other fields).
Answer by Captain_Pineapple · Dec 09, 2021 at 08:41 PM
A few things are to be noted about your code:
First up is that you should not have a while loop with the condition != 0f
This is not safe imo when dealing with floats. Please go for >= 0f
instead.
Secondly why WaitForSeconds(PadTime / PadTime);
This is WaitForSeconds(1f);
right?
But most importantly:
StopCoroutine(PadTimer());
This does not do what you think it does. If you want this coroutine to end then just exit the function. This can be forced with yield break;
as a similarity to return
in a normal function but just letting the function end like any other as well is enough.
If you want to use StopCoroutine
correctly you have to do it like this:
routineReference = PadTimer()
StartCoroutine(routineReference);
...
// do something and somewhere else cancel it:
...
StopCoroutine(routineReference);
Most importantly your issue is Probably that you do not reset PadTimer once it has reached zero. This is probably the issue why you cannot restart it since it always directly exits your Routine.
Answer by Hellium · Dec 09, 2021 at 08:57 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimedPressurePad : MonoBehaviour
{
public float padTime = 5f;
public Material pressed;
public Material unpressed;
public Renderer padRenderer;
// Fill the inspector with the one, two, three, four and five pads
// First element = [0..1[ second
// Second element = [1..2[ second
// ...
// Fifth element = [4..5[ second
// ...
public Material[] timerMaterials;
private void OnTriggerEnter(Collider other)
{
StopAllCoroutines();
StartCoroutine(ChangePadMaterial());
}
IEnumerator ChangePadMaterial()
{
int index = Mathf.Min(Mathf.FloorToInt(padTime - 0.01f), timerMaterials.Length - 1);
if(index >= 0 && !Mathf.Approximately(index, padTime))
{
padRenderer.material = timerMaterials[index];
yield return new WaitForSeconds(padTime - index);
index--;
}
WaitForSeconds wait = new WaitForSeconds(1);
for(; index >= 0 ; index -= 1)
{
padRenderer.material = timerMaterials[index];
yield return wait;
}
padRenderer.material = unpressed;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613050310im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I store data from a timer? 1 Answer
Creating a Timer 0 Answers
more resource efficient TIMER 2 Answers
how to make a varied countdown timer 1 Answer