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 nolan_oc · Jan 08, 2021 at 05:29 PM · coroutinesienumeratorloopingwhile loop

Unity while loop causing freezes on play

I assume this code is being read as an infinite loop, but I cannot figure out why.

I want this code to run until a GameObject is destroyed which would trigger the GameObject enemyTarget to return as null, breaking the loop.

I've force quit and rebooted my project about 20 times with new attempts but it's become clear to me that I'm missing something. Really looking for a second opinion here. This is the code. StartCoroutine("Attack");

 public IEnumerator Attack()
     {
         while (enemyTarget != null)
         {
                 enemyTarget.GetComponent<EnemyStats>().health -= attackDamage;   //do damage
                 yield return new WaitForSeconds(attackSpeed);
         }
         LocateTargets();    //return to searching for targets
         yield return null;
     }
Comment
Add comment · Show 5
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 unity_ek98vnTRplGj8Q · Jan 08, 2021 at 05:59 PM 1
Share

I don't see anything immediately wrong here, but there's a few things you can try so you can figure out what's going on.


  1. Only execute the while loop a few times then break out of it

  2. Set the attack speed to be reeaallly slow

  3. Get rid of LocateTargets at the end and see if that makes a difference

  4. $$anonymous$$ake sure you are only calling the coroutine ONCE

  5. If you are using Visual Studio, you should be able to attach to Unity and debug line by line

avatar image nolan_oc unity_ek98vnTRplGj8Q · Jan 08, 2021 at 07:49 PM 0
Share

Thanks for the reply I'm pretty confused. Going to try 1 in a bit, just as a failsafe I suppose 2 & 3 had no effect on the freezing up 4 the Coroutine is called once per enemy in the scene so I guess technically multiple times, but only once per enemy. Would that matter? 5 I'll look into setting this up, sounds pretty useful thanks

avatar image unity_ek98vnTRplGj8Q nolan_oc · Jan 08, 2021 at 08:42 PM 1
Share

Then yeah 5 is your best bet. Even if the enemy is never destroyed Unity shouldn't freeze up as long as you have a yield return in there. I suspect something else is happening. Unity doesn't freeze up until Attack is called, right?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CmdrZin · Jan 08, 2021 at 06:43 PM

Where do you Destroy the enemyTarget so that it will be 'null'?
You also might want to move the LocateTargets() to a update method.

Comment
Add comment · Show 1 · 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 nolan_oc · Jan 08, 2021 at 07:42 PM 0
Share

enemyTargets are destroyed in the Update() of EnemyStats.cs if their health <= 0

avatar image
0

Answer by Llama_w_2Ls · Jan 08, 2021 at 09:35 PM

Where do you start the coroutine? If you start the coroutine in the update, it will start a while loop every frame, which will definitely cause a crash. @nolan_oc

Comment
Add comment · Show 1 · 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 nolan_oc · Jan 08, 2021 at 09:56 PM 0
Share

I've been calling it at the end of the LocateTargets(), would that be technically an infinite loop? At Start I call LocateTargets() and if any are found it goes to Attack() and then back to LocateTargets(). I'll test this out and get back to you. Thanks for the tip

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

117 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

Related Questions

Animated text, word by word not letter by letter - coroutine 1 Answer

IEnumerator OnTriggerEnter2D IF statement 2 Answers

Coroutines IEnumerator not working as expected 2 Answers

Make a function that can return string or IEnumerator 2 Answers

Coroutine Not Working 2 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