Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Klasmic · Jul 17, 2017 at 03:55 PM · crasheswhilewhile-loopwhile loop

While loop freezes unity

I'm trying to create an AI that goes to the player, I have a script that is supposed to move the zombie to the player but the zombie didn't move and when I move to the zombie unity freezes, here's my script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Deformed : MonoBehaviour {
 
     Rigidbody2D rb2D;
     Rigidbody2D playerRb2D;
     Transform trans;
     Transform playerTrans;
     public float Distancebetween;
 
     public GameObject player;
 
     void Start () {
         rb2D = gameObject.GetComponent<Rigidbody2D> ();
         trans = gameObject.GetComponent<Transform> ();
         playerTrans = player.GetComponent<Transform> ();
         playerRb2D = player.GetComponent<Rigidbody2D> ();
     }
     
     // Update is called once per frame
     void Update () {
         Distancebetween = playerTrans.position.x - trans.position.x;
 
         while (Distancebetween > 0) {
             rb2D.AddForce (Vector2.right * playerRb2D.velocity.x);
 
         }
     }
 }


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 jtxeka · Jul 17, 2017 at 04:35 PM 0
Share

You can't use while there. You are not modifying Distancebetween inside the loop, once you are inside the while, you'll never exit. Anyway AddForce don't modify immediately the object position. Try with "if" ins$$anonymous$$d "while" and you can check how AddForce works. You'll see a very fast zombie.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by dragonking300 · Jul 17, 2017 at 04:23 PM

Try changing the while loop to a if loop like

 if(Distancebetween > 0) {
 rb2D.AddForce (Vector2.right * playerRb2D.velocity.x)
 }
 else
 {
 //code
 }
 }
 }

Because unity will crash if the loop is infinite

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

Answer by Dorscherl · Jul 17, 2017 at 05:11 PM

I'm still new to Unity but I know that Update is a loop of it's own. It is called every frame. My theory is that your while loop prevents Unity from going to the next iteration (frame). Not only that, your while loop is an infinite loop and in ANY types of programming that is an error. Your loop control variable isn't being modified in the loop.

I would use an If statement

 if (Distancebetween > 0)
 {
     rb2D.Addforce( Vector2.right * playerRb2D.velocity.x);
 }

This way your script won't hinder itself.

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

Answer by Sneakyshot4012 · Aug 13, 2017 at 01:42 AM

I believe that while loops freezes Unity because the Update() function is basically a while loop of it's own. You will need an if statement to see if the enemy is close to the player, not a while.

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

71 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

Related Questions

Vector3.MoveTowards becomes instant in while loop or freezes unity 1 Answer

Using a while loops to repeatedly execute a method? 2 Answers

Quaternion.Slerp issue 1 Answer

How to get something to be inactive while there is a certain value 2 Answers

Iterator variable not increasing. 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