Question by
mirek.um · Sep 03, 2015 at 08:36 AM ·
waitforsecondscoroutines
Do something for 10 sec
How to continue doing something for lets say 10 seconds and then break. Do I need to use coroutines? And how? eg:
for (int i=0; i<10000; i++)
{
Debug.Log(i);
// break after 10 sec
}
Comment
Best Answer
Answer by Positive7 · Sep 03, 2015 at 09:13 AM
public float tenSec = 10;
public bool timerRunning = true;
int i;
void Update(){
if(timerRunning){
tenSec -= Time.smoothDeltaTime;
if(tenSec >= 0){
Debug.Log(i++);
}else{
Debug.Log("Done");
timerRunning = false;
}
}
}
or with Corutine :
void Start ()
{
StartCoroutine(StartCounter ());
}
private IEnumerator StartCounter()
{
countDown = 10f;
for (int i=0; i < 10000; i++)
{
while (countDown >= 0)
{
Debug.Log(i++);
countDown -= Time.smoothDeltaTime;
yield return null;
}
}
}
The part with coroutine is exactly what I need, Thanks :)
Answer by Qasem2014 · Sep 03, 2015 at 10:28 AM
when i want to count , i use this code :
var Sec:**float**;
function Update()
{
if(Sec <= 10)
{ Sec += 1*Time.deltaTime; print(Sec); }
}
it increase the Sec variable 1 unit in every second .