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 safetyfirststudios · Oct 19, 2020 at 09:36 AM · timefloatif-statementswhile-loopbasic programming

Unity freezes after running this basic code.

 private void OpenDoorAnimation()
     {       
         float startTime = Time.time;
         float timeDiff;
         int i = 0;
 
         while (i < 23)
         {
             timeDiff = Math.Abs(Time.time - startTime);
             if (timeDiff > 5.0f)
             {
                 i++;
                 startTime = Time.time;
             }
         }
     }

Unity always freezes after the code reaches the while loop. Been staring at this for hours, Im either brain dead or going insane. Send help.

Basically, a button is pressed, and another method is called, that then calls this method. Absolutely nothing else happens in the code. I know the button works because ive tested it with multiple Debug.Log methods. But it can never reach the Debug.Log message that I would put inside the loop. Unity freezes before that happens. I always have to force close unity with task manager and restart it.

Your probably wondering why the method is called 'OpenDoorAnimation'. Well inside the if-statement, its suppose to switch to the next sprite every 5 seconds, from sprites in an array. And you guessed it, there is 24 different sprites. Hence the i < 23. But that doesnt really matter, cause Unity wants to freeze every time I run this simple loop.

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

2 Replies

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

Answer by Bunny83 · Oct 19, 2020 at 10:11 AM

Of course it will freeze. Though not after running your code but while your running your code because you have an infinite loop. Lets break it down:


Your while loop will iterate as long as i is smaller than 23. Inside your while loop you only ever increase i inside your if statement. That means when the if statement is false you will not enter the if statement and you're looping in the while loop for ever.


So the next question is when is your if statement actually true? The answer is also quite simple: never. That's because you trapped Unity inside your while loop. Time.time gives you the time at the start of the current frame. It does not change during the execution of the current frame. Since you are stuck in your while loop without ever returning the control back to Unity, Unity will never complete the current frame and can never update the time property.


Even if you used an asynchronous time source (like System.DateTime.Now) Unity would still be frozen during that "wait" time.


You may want to use a coroutine for this. Though you don't seem to currently use your "i" value anywhere. So it's difficult to give an example since your loop currently is a "useless" loop.

I would probably use something like this:

 private void OpenDoorAnimation()
 {
     StartCoroutine(OpenDoor());
 }
 private IEnumerator OpenDoor()
 {
     for(int i = 0; i < spriteArray.Length; i++)
     {
         // use i here to change your sprite
         yield return new WaitForSeconds(5f);
     }
 }
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 mgw2021 · Oct 08, 2021 at 02:35 AM

I try to run the testing and it block the UI updating. I expect text must be updated then only execute the while looping, but it is not. i found out that once the following scripts completed and unity only run UI updating for "this.Button.GetComponentInChildren().gameObject.SetActive(false);" and "Debug.Log". any idea how to resolve this issue?

public void Click() {

     this.Button.GetComponentInChildren<Text>().gameObject.SetActive(false);

       float time = 0;
       while (time <= 1000000)
        {
                time++;
                 Debug.Log("Im doing heavy work");
         }

}

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

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

Related Questions

OpenFeint float scores 2 Answers

Syncing times 0 Answers

How to add a cooldown sort of thing. 1 Answer

Changing a float number by time 1 Answer

if(float == Time.time) not working, why? 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