- Home /
start coroutine everytime
hello,
I am working on space shooter like game.
I want to spawn enemies with frequent time delay.
My Question is should i start coroutine everytime, or place a while loop in one coroutine so that i do not have to start coroutine everytime ??
which is better?
does it(while loop option) affect in performance issue or not??
To do something freqently with a delay you can always use InvokeRepeating("FunctioName",startdelay,repeattime);
void Start()
{
//Will Instantiate a new enemy every 2 seconds
InvokeRepeating("SpawnEnemies",1.0f,2.0f);
}
//Function to spawn enemies
void SpawnEnemies()
{
GameObject Enemy = Instantiate(EnemyPrefab,transform.position,transform.rotation);
}
thanks sethuraj for ur answer.
i know about Invoke Reapeating.
but InvokeRepeating will spawn enemy with static time(2 seconds).
my requirement is like, Initially there should be Timedelay of two seonds. but as my ship travels higher, i want to reduce TimeDelay then?
Answer by RedDevil · Jul 25, 2014 at 06:39 AM
I just put my function:
void Start()
{
StartCoroutine(FunctionName());
}
IENumerator FunctionName()
{
while(true)
{
yield return new WaitforSeconds(time you want);
instantiate enemy
yield return new WaitforSeconds(time you want);
}
}
I found this to work better then invoke repeating
Answer by Tehnique · Jul 25, 2014 at 06:27 AM
Sounds like you need to Invoke something Repeatedly....ta-daa: InvokeRepeating
yes exactly Technique.
but again my problem is as above as i have commented in sethuraj's answer.
If you need to invoke it 10 times for 2 seconds, then 10 times for 3 seconds and so on, you can use CancelInvoke to stop all current invokes, and then InvokeRepeating again to define a new time interval.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154937im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Does a coroutine do all its work during one frame? 1 Answer
UI Text not changing color [C#] 2 Answers
Rotate GameObject using Coroutine 2 Answers
Coroutine loop not working 1 Answer