- Home /
while(true) ???
im confused. I kow how a while loop works and what it does but im stumped on this line while(true)... while what is true??? i dont get it. here is the code that is in question copied from the fps tutorial ai sript.
function Start () {
// Auto setup player as target through tags
if (target == null && GameObject.FindWithTag("Player"))
target = GameObject.FindWithTag("Player").transform;
animation["idle"].wrapMode = WrapMode.Loop;
Patrol();
}
function Patrol () {
var curWayPoint = AutoWayPoint.FindClosest(transform.position);
while (true) {
var waypointPosition = curWayPoint.transform.position;
// Are we close to a waypoint? -> pick the next one!
if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
curWayPoint = PickNextWaypoint (curWayPoint);
// Attack the player and wait until
// - player is killed
// - player is out of sight
if (CanSeeTarget ())
yield StartCoroutine("AttackPlayer");
// Move towards our target
MoveTowards(waypointPosition);
yield;
}
}
so what is going on here can someone please explain.
Answer by DaveA · Mar 24, 2012 at 05:18 PM
It means 'do this loop forever' which, unless there were yields there (or early returns), would hang a program. In this case, it means this script will execute those commands until the game stops or the object it's on is destroyed or disabled.
so once you yield the method would need to be called again through and update() or ect.
You should use 'add new comment' rather than Answer. I assume you're asking me. No, the framework takes care of it. Yield basically says 'go off and do other things, like process events and play sounds or whatever, but come back to this point in this method and continue from there'
Answer by Eric5h5 · Mar 24, 2012 at 05:17 PM
A while loop continues to loop as long as the condition is true. Since true is always true, while (true) is an infinite loop.
Your answer
Follow this Question
Related Questions
Problem with while statement. 1 Answer
How come my boost comes to an abrupt stop? 1 Answer
While loop crashing 2 Answers
Unity crashes when using while 2 Answers
Changing a string in a loop each second 3 Answers