- A method returning an
IEnumerator
is one which can use theyield
instruction to return values to the caller. Whenever the program flow reaches ayield
, the program flow goes back to the caller. The caller can then process that value and then resume the execution of the enumerator-method after theyield
instruction (or not). When you encounter such a method in a Unity project, then it is usually used for a Coroutine. Theyield return
's in a coroutine are the points where the program flow returns to the engine which will then resume the coroutine at a later time (how much later depends on what value youyield return
).A method returning an
IEnumerator
is one which can use theyield
instruction to return values to the caller. Whenever the program flow reaches ayield
, the program flow goes back to the caller. The caller can then process that value and then resume the execution of the enumerator-method after theyield
instruction (or not). When you encounter such a method in a Unity project, then it is usually used for a Coroutine. Theyield return
's in a coroutine are the points where the program flow returns to the engine which will then resume the coroutine at a later time (how much later depends on what value youyield return
).Unity Coroutines can be an elegant way to express processes which are supposed to take multiple frames to complete without having to add complicated state-determination logic to your
Update
methods or opening up the box of pandora which is actual multi-threading (I personally prefer state machines, command queues and behavior trees, but that's a personal preference). The
a ? b : c
construct in C# is the Ternary Condition Operator. It returns a single value. Ifa
istrue
, it returnsb
, otherwise it returnsc
. In this casea
isTarget.y == Top.y
. So in plain English, this line means "IfTarget.y
is equalTop.y
,Direction
is up, otherwise Direction is down".And so,
Vector3 newTarget = Target.y ==Top.y ? Bottom : Top;
means "IfTarget.y
is equalTop.y
, then head towardsBottom
, otherwise head towardsTop
"
Unity Coroutines can be an elegant way to express processes which are supposed to take multiple frames to complete without having to add complicated state-determination logic to your Update
methods or opening up the box of pandora which is actual multi-threading (I personally prefer state machines, command queues and behavior trees, but that's a personal preference).
2. The a ? b : c
construct in C# is the Ternary Condition Operator. It returns a single value. If a
is true
, it returns b
, otherwise it returns c
. In this case a
is Target.y == Top.y
. So in plain English, this line means "If Target.y
is equal Top.y
, Direction
is up, otherwise Direction is down".
3. And so, Vector3 newTarget = Target.y ==Top.y ? Bottom : Top;
means "If Target.y
is equal Top.y
, then head towards Bottom
, otherwise head towards Top
"