Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 joaopedroferreira0318 · Jul 31, 2020 at 05:09 PM · while-loop

infinite while loop checking if a gameObject is in the air

I'm trying to code a push/pull mechanic, and when the player pushes the box off the ledge I want to keep it not kinematic while it doesn't reach the floor. For that, I'm using a while loop inside Update() that checks, with raycasts, if the box is still in the air, but it crashes the unity.

 void Update()
     {    
 
         if (Input.GetKeyDown(KeyCode.LeftShift) && PushableObject() && GroundCheck())
         {
             box.GetComponent<Rigidbody>().isKinematic = false;
             box.gameObject.transform.parent = player.transform;
             isPushing = true;
            
         }
 
         if ((Input.GetKeyUp(KeyCode.LeftShift) && isPushing) || (isPushing && PushableObject() == false))
         {
 
             if (BoxGroundCheck(box) == false)
             {
                bool onGround = false;
 
                 while(onGround == false)                 
                 {
                     box.GetComponent<Rigidbody>().isKinematic = false;
                     onGround = BoxGroundCheck(box);
                  }            
             }
 
             box.GetComponent<Rigidbody>().isKinematic = true;
             box.gameObject.transform.parent = null;
             isPushing = false;
         }
     }
 
 bool BoxGroundCheck(GameObject bx)
     {
         RaycastHit hit;
         float distance = 0.5f;
         Vector3 dir = new Vector3(0, -1);
         bool isGrounded;
 
         if (Physics.Raycast(bx.transform.position, dir, out hit, distance) || Physics.Raycast(bx.transform.position + new Vector3(-0.2f, 0, 0), dir, out hit, distance) || Physics.Raycast(bx.transform.position + new Vector3(+0.2f, 0, 0), dir, out hit, distance))
         {
             isGrounded = true;
         }
         else
         {
             isGrounded = false;
         }
 
         return isGrounded;
     }
 
 bool PushableObject()
     {
         RaycastHit hit;
         float distance = 1f;
         bool isPushable;
 
         if (Physics.Raycast(player.transform.position, new Vector3(x, y, z), out hit, distance))
         {
             if (hit.collider.gameObject.tag == "Box")
             {
                 isPushable = true;
                 box = hit.collider.gameObject;
             }
             else
             {
                 isPushable = false;
             }
         }
         else
         {
             isPushable = false;
         }
 
         return isPushable;
     }
Comment
Add comment · Show 1
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 UnityedWeStand · Jul 31, 2020 at 11:06 PM 0
Share

The answer below is correct. I would just like to emphasize an important detail, which is as long as the while loop is running, Unity does not execute any Update() or FixedUpdate() functions. Because you are using physics to check if the box is on the ground, you need to keep running FixedUpdate() while you are checking if the box is on the ground, so you cannot use this while loop structure.

1 Reply

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

Answer by cwalshwarder · Jul 31, 2020 at 10:53 PM

When you do a loop, unity needs to finish it before it can do anything else. Maybe you can just do that each Update() or each FixedUpdate(), without putting it in a while loop. Maybe you need to start a coroutine where the while loop is.

While you're fixing this, to avoid crashes, you could add a variable to which you add 1 in the while loop, and stop the loop & Debug.Log("infinite loop") if it's over like 1000.

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 highpockets · Jul 31, 2020 at 11:14 PM 1
Share

I agree 100%. The update loop is already a while loop that doesn’t stall the thread unless you put an infinite loop inside it. If you need to wait for something to complete before continuing, you either have a condition in the update method. Or use a coroutine, which is also on the same thread as everything else running in Unity, but allows you to yield and wait for something to finish by means of one of the WaitFor methods or by use of a while loop with a yield statement inside.

avatar image joaopedroferreira0318 highpockets · Aug 01, 2020 at 02:11 AM 0
Share

Whoa, thank you for those tips. I didn't know about coroutines yet. I did this and it worked well:

 void Update()
     {
          ...
             if (!BoxGroundCheck(box))
             {
                 box.gameObject.transform.parent = null;
                 StartCoroutine("KeepNotKinematic");
             }
          ...
     }
 
 IEnumerator KeepNotKinematic()
     {
         while(!BoxGroundCheck(box))
         {
             box.GetComponent<Rigidbody>().isKinematic = false;
             BoxGroundCheck(box);
             yield return null;
         }
     }

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

133 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

javascript: declaring a variable in 'while' loop 2 Answers

unity bug??? while loop is satisfied, but doesn't quit 2 Answers

While loop issues in Update() vs. Start() 0 Answers

While loop variable assignment not workin 0 Answers

While Loop Infinite... Rookie Mistake 1 Answer


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