private void Start()
{
StartCoroutine(RespawnDelay());
}
void FixedUpdate()
{
IEnumerator RespawnDelay()
{
yield return new …
//stuff
}
}
this doesn’t work, I was wondering how this could be fixed
private void Start()
{
StartCoroutine(RespawnDelay());
}
void FixedUpdate()
{
IEnumerator RespawnDelay()
{
yield return new …
//stuff
}
}
this doesn’t work, I was wondering how this could be fixed
You tried to define a method inside another method. This is in general not possible in C#. The only excpetions would be lambda expressions and closures / anonymous methods. So move the declaration of your RespawnDelay coroutine out of the FixedUpdate body.
Your title doesn’t seem to be related to the code in question at all. You currently “call” / start the coroutine inside your Start method. Of course you could call StartCoroutine( RespawnDelay() ) from Update but that’s generally not recommended and probably is not what you want to do anyways. Calling StartCoroutine from Update means you start a new coroutine every frame that is independent of all the other coroutines you have already started. You can not make the Update method “wait”. If you want some sort of spawning loop with a relatively long delay between the spawns it’s generally better to use a coroutine which you start once in Start and then just use an infinite loop inside the coroutine to keep it running. However the importent thing is if you use an infinite loop you have to make sure you always have at least once yield instruction inside the infinite loop.
Something like that:
private void Start()
{
StartCoroutine(RespawnDelay());
}
IEnumerator RespawnDelay()
{
while (true)
{
yield return new WaitForSeconds(10);
//stuff
}
}