Problems with yield

Having some problems with the yield thingy, am undoubtedly doing it wrong but I can’t see any difference between my code any the examples online lol

Any help greatly appreciated!

(using javascript)

Code:

yield Ani.Mate.By(this.transform, 0.5, {“position”: new Vector3(0,0,10)});
this.transform.position.z = transform.position.z+10;

yield WaitForSeconds(0.5);

Error: BCE0070: Definition of ScriptName.FunctionName depends on ScriptName.FunctionName whose type could not be resolved because of a cycle. Explicitly declare the type of either one to break the cycle.

Ok well I found out what part of the problem was, the function using yield has the ability to call itself, which Unity apparently doesn’t like sigh

Unfortunately I need this function to be able to either call itself or return a boolean value but it wants me to make it return an IEnumberable or an object??? I don’t really understand this, can anyone help me here by either explaining how to do this?

Any kind of help right now would really be appreciated…

do you return correctly?

From coroutines you return through yield return value

not through return value as in common functions

Oh I’m probably not doing that properly, could you post a short sample of code to demonstrate how I would do this please?

Thanks in advance.

Example to demonstrate what?
Returning a value? Likely you don’t do that, you would directly set the variable value and use

yield break;

to leave the coroutine. Thats simpler and more straight forward.

The unity documentation and script documentation on yield coroutines will explain the whole topic more indepth and potentially answer questions you might have or which might come up :slight_smile:

I’ve tried the documentation - there is literally 1 sentence on yield and it doesn’t explain anything lol so far I’ve found the documentation to be absolutely useless, which isn’t good since I have no understanding of coroutines or how to use yield properly or anything!!!
This is supposed to be for my degree project too… :frowning:

And I’m still confused: I get compiler errors -missing ;- if I put “yield break;” into my code?

I’m sorry I’m fully aware that I’m a pest lol I just don’t understand how this is working (or not) at all!

There is not much you have to know on the whole Coroutine topic. It is actually pretty simple, from what it does and from the understanding point.
At best you search for YieldInstruction in the script reference that will show some of the important commands for further documentation.

StartCoroutine, StopCoroutine and the other commands you find in their script documentation are all you need to get the coroutines working. Their scripting documentation is likely also the point to start if you need to get any deeper understanding althought the system is pretty simple as it is just a script scheduling system (not multithreading), where yield is the instruction to init a reschedule of the coroutine you are in or stop it alltogether from within the coroutine.

pausing is done through the WaitForxxxx commands (there are different ones).
Ending the coroutine is done through yield break;

One of the most important places where yield is used especially is the WWW class if you download stuff as yield will make the script wait until the download has finished so you have the data at hand

Ok I’ll look into those further, thanks for all your help!

Sure no problem.
Wish you good luck with your project.

Oh and before I forget it: my code is commonly C# so if you are using JS you might have to change details of it.