- Home /
timer not ticking down
hi
i am making a simple bomb script that needs to explode walls in its surounding area. but my timer is not going down. i tested the exact same timer on diffrent prefabs and object but it doesn't work. pls help.
this is my code:
public class bomb_explode : MonoBehaviour
{
public float T=2; //timer
public LayerMask layer;
private void FixedUpdate()
{
T -= 1 * Time.fixedDeltaTime; //goes down every second
}
void Update()
{
if (T < 0)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 10, layer); //gets every collider of object in radius in the array
Debug.Log(colliders.Length);
foreach (Collider2D col in colliders) //for loop until lengt of array is reached (int i = col)
{
Destroy(col.gameObject); //destroy the object attached to that collider
Debug.Log("wall down");
}
Destroy(gameObject); //destroy your self
}
}
}
Answer by tormentoarmagedoom · Sep 29, 2019 at 06:20 PM
Hello there.
I supose (for yout script) you dont know about Corutines.
These are methods (functions) where you can use the "yield" command. For example, lets make a simple script (dont be afraid, they are veeery simple):
void Start()
{
StartCoroutine(Bomb());
}
IEnumerator Bomb()
{
yield return new WaitForSeconds(5);
// BOUM!! (Epxlosion code)
yield return null;
Debug.Log("Bomb made boum boum");
}
What this script does is very simple. At the start, the corutine bobm is called. First, we have a yield order, so it will wait for X time before continue the script (in our case 5 seconds). Then the bom explode. Then we yield again for "null time" this means, it will just wait for the next frame to continue. So the next frame after bom explode, we will have the message in console.
Corutines are very important to make our project dont think too much in the same frame, and to control "timing".
You should take you time making tests, watching tutorials about this, and then continue with your project. You will see corutines are very BASIC AND IMPORTANT functions.
Good luck!
Your answer
Follow this Question
Related Questions
CountDownTimer Help 1 Answer
How do I make a Countdown Timer in Minutes and Seconds only 1 Answer
How to make this display milliseconds? 3 Answers
Timer Continues After Object is disabled 2 Answers
Timer counting UP instead of DOWN 1 Answer