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 ic3t3a2000 · Mar 05, 2013 at 05:49 AM · looploopingwhile

while loop issue

I can't figure out why I am freezing unity with my while loop. I am trying to check the sate of a boolean variable in another script called "IsObjectThere". If that Boolean variable is not true and if the mouse button is up then I it to remain in the while loop until the canSpawn variable is true. Once its true I want to break the while loop.

If I run my code without the while loop it runs fine. As soon as I enter the while loop it freezes unity. Below is the part of my code I am dealing with.

 var objs : GameObject[];
 var respawn : GameObject;
 var IsObjectThere : isObjectThere;
 var waiting : boolean;
 
 private var obj : GameObject;
 
 function Start()
 {
     if (respawn==null) 
         respawn = GameObject.FindWithTag ("Respawn");
 }
 
 function Update()
 {
     
     if (Input.GetKeyDown (KeyCode.Mouse0))
         {  
            obj = Instantiate(objs[(Random.Range(0, objs.Length))], respawn.transform.position, respawn.transform.rotation);
            obj.transform.parent = Camera.main.transform;
            IsObjectThere = obj.GetComponent(isObjectThere);
         }
         
         
         if (Input.GetKeyUp (KeyCode.Mouse0)&& IsObjectThere.canSpawn)
         {
             DropObject();
 
         }
         if(Input.GetKeyUp (KeyCode.Mouse0) && (!IsObjectThere.canSpawn))
         {
             waiting = true;
             
             while(waiting)
             {
                 if (IsObjectThere.canSpawn)
                 {
                     waiting = false;
                 }
             }
             
         }    
 }
 
 function DropObject ()
 {
     obj.transform.parent = null;
     obj.GetComponent(Rotate).enabled = false;
     obj.gameObject.tag = "placedObject";
     Debug.Log (IsObjectThere.canSpawn);
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Shiroi24 · Mar 05, 2013 at 06:37 AM

Unity "Freezes" because it is stuck in a while loop during update(). And since update() never finishes, Unity never gets to the next step until waiting is false. But since Unity can't update again(because it is already stuck in an update), IsObjectThere.canSpawn will never be updated and causes Unity to get stuck.

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 ic3t3a2000 · Mar 05, 2013 at 03:36 PM 0
Share

Whats the best way to handle a situation like that? If you could give me an example of how I might proceed that would be greatly appreciated.

avatar image Shiroi24 · Mar 05, 2013 at 03:52 PM 0
Share

I would separate out waiting and not waiting with if statements.

 if(waiting)
 {
      codeForWhileWaiting;
 }
 else
 {
      codeForWhenNotWaiting;
 }

Either that or I would use a finite state machine.

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

12 People are following this question.

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

Related Questions

How do i make it rain spheres continuously in a loop in Cardboard SDK? 0 Answers

Infinite Loop in void checkcard (memory game) 1 Answer

Sound not looping properly on device? 2 Answers

How To Wrap A Blender Animation??? 1 Answer

Unity Movie Texture - Skipping First or Last Few Frames 0 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