A lot of people say that the do-while construct is not supported in UnityScript. I didn’t know that, so I wrote this code to test why I was getting wierd results.
function Start () {
var count = 0;
do {
print("do-while(true): " + count++);
yield;
} while(true);
count = 0;
do {
print("do-while(false): " + count++);
yield;
} while(false);
}
That’s not what happens though. I copy/pasted your code, and do/while(true) runs forever as expected. I suspect you’re actually running a different version of the code.
Yea any loop which does not exit will crash unity. This includes stuff like properties which gets call the set or functions which call themselfs, or while/do-while/for
Not necessary; just “yield” is fine. The problem comes when using an infinite loop with no yield at all, which is not the issue in this topic.
I actually tested the code earlier in 3.2 and it worked fine, however I neglected to recompile it. Interestingly it works in 3.2 as long as you don’t recompile the scripts. Indeed it’s 3.4 that has real proper support.
I use a basic do while script as my AI for most all of my constructs. It seems to work fine, they change stare with out crashing unity at all. I also use yield.
This is an example of yield script for a basic FSM ai, but it helps to use a cool down when you have multiples of these going
IEnumerator StartFSM()
{
while (true)
{
switch (state)
{
case State.StartUp:
StartCoroutine(StartUp());
break;
case State.CheckPlayersTowers:
CheckPlayerTower();
break;
case State.CheckResources:
CheckResources();
break;
case State.CheckMyTowers:
CheckMyTowers();
break;
}
yield return new WaitForSeconds(coolDown);
Probably it was a bug in the UnityScript compiler backend. I didn’t look the CLI generated, but I guess that it was using the wrong instruction in the condition evaluation.
If you translate the code to C# the compiler will warn you about unreachable code after the first do-while, so it’s safe to say that was a compiler bug.