Hi all, right I’m trying to use co routines so I can have some script running about every 30 seconds. It either seems to get called every frame or not at all.
So I went to the unity documentation page for co routines and copied & pasted a example from there & I get the same thing.
If I try and put yield return waitandprint in update I get this error
Assets/pool.cs(109,14): error CS1624: The body of pool.Update()' cannot be an iterator block because void’ is not an iterator interface type
I also tried some other unity examples, no luck there either.
I haven’t got a clue what Im doing wrong, any help appreciated
To expand a little more on that: StartCoroutine dispatches the function to the coroutine processor. You can’t keep some kind of handle on it through Example() (well, okay, you can, but considering you’re starting out with this, K.I.S.S.). You can’t treat a coroutine call as a simple function call though, they are very different things.
Update() is called every frame, so you’re starting a new Coroutine in every frame. Place StartCoroutine() in Start(), or use conditionals in Update() so it can only be called once and when it is needed.
I thought the whole point of co routines is so they can be repeated but not every frame. I suppose I could use the Time.deltaTime but I’m going to need to use co routines sooner or later so thought I might as well start using them now.
I tried it again, I put the start coroutine line in start and it gets called once only. If I put it in update it does wait for the specified time but then repeats every frame. Obviously theres something I’m missing just don’t know what. I would have thought using the example from the official unity page wouldn’t have given me any problems. Guess I’ll just have to keep reading & trying.
@ cmcpasserby - I’m just using the time.deltatime to run the code every 20 seconds, I’m sure I’m going to need co routines in the future so I’ll be reading up on them but for now what I’ve done is sufficient, thanks for the replys.
@ GregMeach - Thanks I’ve already seen that video but I’ll have another look in more detail.
You can actually make your Start() function into a simple coroutine like so:
IEnumerator Start()
{
for (int i = 0; i < 5; i++)
{
// <WIP> Do Something Here...
yield return new WaitForSeconds( 1.0f);
}
Destroy(this);
}
I use the above pattern for scripts that run for a certain lifetime and then are over with, fire-and-forget type things.
NOTE: I threw in the Destry() call at the bottom on the script. If you want to Destroy the entire game object you would use Destroy(gameObject) obviously. This kills the game object.
The main caveat is, MAKE SURE YOU YIELD! If you do this:
while(true)
{
// <Do Things>
}
You will be very unhappy because Unity will lock up.
Assuming your statement is serious, I have no idea where this “avoid using Update()” meme got started but there is nothing wrong with using Update() to do something every frame. That is precisely what it is for.
If you make a coroutine that does something and yields for one frame, and do the same amount of work in an Update() call, you will see exactly zero performance difference for any reasonable amount of game objects that you could ever conceive of having.
I completely agree and I’m unsure what your point is, but my point was that you should minimize processing in Update(). It was specifically in response to:
Which would imply that operations that are not needed every frame should still be put into Update() with conditionary logic. Which is just awful. So I suppose what I mean by “avoid Update() like the plague” is “minimize per-frame operations as much as you can”.