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 /
This question was closed Feb 22, 2020 at 06:01 PM by Kamisama_of_Pimpness for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Kamisama_of_Pimpness · Feb 21, 2020 at 04:13 PM · coroutineif-statementscrashes

Coroutine freezes editor but only sometimes. Why ? Why is it happening randomly ?

Really weird behaviour. At some test runs, this coroutine freezes the unity editor at the point when it is supposed to check if(i == 2), that I have to terminate unity editor via task manager every time. And at other test runs, it runs without any problems without having changed any code. It's unpredictable for me to why it crashes randomly at that particular if statement. It's driving me into madness especially since it does work but sometimes it crashes unity without any debug messages are getting printed. I really need some suggestions. Am I misunderstanding yields or something ? I'm using Unity2019.2.17f1

Edit1: Not crashing, but rather freezing to the point task manager says the editor gives no response. Also added 2nd screenshot how the coroutine is being called. Its always being called once in that scripts lifetime. Edit2:It's worthy to mention that the gameobject which houses this script gets destroyed and instantiated as clone. I made sure that there can only one of its kind exist in a scene and also made sure all coroutines on that monobehaviour get stopped OnDestroy. alt text

alt text

anmerkung-2020-02-22-040655.png (64.9 kB)
anmerkung-2020-02-21-145043.png (33.9 kB)
Comment
Add comment · Show 2
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 Bonfire-Boy · Feb 21, 2020 at 04:51 PM 1
Share

Does it freeze or crash?

Does the Editor log hold any clues?

How do you know it's happening at that statement? And what's the value of i when it happens?

Seems likely to me that it's actually something that's happening when you activate janitorGame

And it's a curious function. Why not just yield for 2 seconds, then set the object active, then yield for another second, wouldn't that have the same effect (and have the advantage of making it clearer what's happening when, when you add logging for instance.

avatar image Kamisama_of_Pimpness Bonfire-Boy · Feb 22, 2020 at 01:55 PM 0
Share

It freezes. That's the correct word yes.

No Editor logs to why it freezes.

I know it is happening at that statement because I put out a print statement before it that told me the current i value in that for loop.

$$anonymous$$aybe, I'll check the janitorgame script for any clues but then again, why does it work sometimes then?

Thats because I want the player position statement to be run atleast 3 times while the screen is dark. I witnessed how it sometimes doesnt place the player at the position but inserts the euler angles correctly. As insurance, I run these 2 lines multiple times while the screen is dark.

This is so paradoxal, it's like the compiler cannot decide if this codes either functions correctly or not but we all now math doesn't change its result behind the equation if nothing on the other side was changed.

3 Replies

  • Sort: 
avatar image
0

Answer by kskjadav007 · Feb 22, 2020 at 04:56 AM

Are you sure that this Coroutine is not running multiple time.alt text


annotation-2020-02-22-102707.png (28.5 kB)
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 rh_galaxy · Feb 22, 2020 at 03:12 PM

The fact that you feel you have to run things "at least 3 times" for it to take indicates a problem, because C#/Unity isn't that random. And in my experience coroutines are a cause for error.

For example in the above answer StartCoroutine could be run more than once because the routine does not start immediately but is put to a list and executed later, so that m_demooroutineisplaying should be set to true before StartCoroutine and not in the coroutine itself to be sure.

If all you do with the coroutine is to wait and set booleans it could be done in Update() with a linear flow in the code.

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 Kamisama_of_Pimpness · Feb 22, 2020 at 05:59 PM

@rh_galaxy @kskjadav007 I've come in time of great news. I figured out what was causing the freeze. It wasn't the coroutine at all. It was a function called SpawnedPuddles() that dwells in the OnEnable of that janitorGame object, which gets set active in posted coroutine. In SpawnedPuddles(), I used a nested loop, there was a foreach-loop inside a while-loop. The problem in that was the value for the while condition sometimes would never met the condition to false because the value could get into the negative numbers inside the nested foreach-loop. Thanks @Bonfire-Boy for suggesting checking out that object's script instead. The reason I was sure it must have been something to do with the coroutine was because all logs ended in there and not thinking that when an object gets set active in one frame, all of its Awake, OnEnable and Start codeblocks of that object also gets read in the same frame.

Thanks for the suggestions!

alt text


correct.png (48.5 kB)
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

Follow this Question

Answers Answers and Comments

132 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

Related Questions

How To Make UI Button 2 Appear Three Seconds After UI Button 1 is pressed 1 Answer

how to start coroutines in the update method? 3 Answers

Coroutines and if statements 1 Answer

Co-routine with loops crashes Unity 3 Answers

Tips on how to handle multiple if statemens 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