Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nemisis83 · Mar 24, 2012 at 05:11 PM · loopwhiletrue

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nemisis83 · Mar 24, 2012 at 09:27 PM 0
Share

so once you yield the method would need to be called again through and update() or ect.

avatar image DaveA · Mar 24, 2012 at 09:29 PM 0
Share

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'

avatar image
4

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges