- Home /
How to add a delay to a bomb explostion
My current code is using UnityEngine; using System.Collections; public class Bomb : MonoBehaviour { private SphereCollider m_myCollider; public Transform m_particles; private float time = 3; private EnemyMove m_Player; void Start() { m_myCollider = transform.GetComponent<SphereCollider>(); } void Update() { if (Time.fixedTime >= time) { if (m_myCollider.radius < 0.02f) { Instantiate(m_particles, transform.position, transform.rotation); m_myCollider.radius += 0.005f; } else { m_myCollider.enabled = false; Destroy(gameObject); } } } public void OnTriggerEnter(Collider other) { if (m_Player == null) { m_Player = other.GetComponent<EnemyMove>(); Destroy(other.gameObject); } } }
But I can't seem to figure out how to add a delay that works every time I place a bomb in the game. I was using the Time.time >= time to work as a delay but as you can tell it's not a very good way of doing this at all. Any help is appreciated :')
Answer by chaosmaker · Oct 03, 2013 at 12:03 PM
http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds.html
void Update()
{
StartCoroutine(PlaceBomb());
}
IEnumerator PlaceBomb()
{
yield return new WaitForSeconds(time); // Delay execution for time seconds
if (m_myCollider.radius < 0.02f)
{
Instantiate(m_particles, transform.position, transform.rotation);
m_myCollider.radius += 0.005f;
}
else
{
m_myCollider.enabled = false;
Destroy(gameObject);
}
}
Thanks that worked perfectly, didn't think it would be so simple (y)
Your answer
Follow this Question
Related Questions
WaitForSeconds Not Working 1 Answer
Timer Script Not Keeping Accurate Time? 1 Answer
Unity 5 - Time counter Up script (millisecond precision) UI 1 Answer
jump timer 1 Answer
Multiple Cars not working 1 Answer