Search Unity

Freeze on Play with No Error

Discussion in 'Scripting' started by Mashimaro7, Jun 25, 2020.

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    I don't really know how to explain this issue, the only thing I can think that I added recently is the GitHub Package. I get a weird bug, no errors or anything, it just sits there loading forever(I waited 15 minutes earlier today). The strange thing is, it doesn't happen all the time, just now I pressed play, it loaded in two seconds, pressed play again, added a capsule collider to my object, pressed play again and it froze. I didn't touch any other settings, i don't know why it only happens sometimes... It freezes at EditorApplication.playModeStateChanged

    Any help would be appreciated, thanks!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    If it gets stuck on loading, then something is not finishing, ie running an infinite loop or infinitely waiting on something else which stucks. This could be caused by:
    • Entering an infinite loop based on a random number generator value. So sometimes it happens, sometimes not.
    • Concurrency problems. Are you using multithreading in any way, shape or form?
    Other than being caused by your own code, it may simply be Unity crashing. So try deleting the libraries folder, or even wiping Unity completely off your computer (back up your project!) and reinstalling it.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    A few things to look at. Check task manager and see if Unity's memory use increases. I usually see this for some infinite loops.

    In Visual Studio, you can add breakpoints (you can probably do this with other ones if you aren't using VS), then you can debug from VS and it will hit the breakpoint to allow you to check your code. Then you can step through and it will help you track down if you are stuck in a loop.

    And of course, just the basic, look for loops that might be trouble areas at startup.
     
    Mashimaro7 likes this.
  4. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Well, I am trying to replicate the issue now, it seems to have fixed itself lol. Maybe I needed a restart? I don't know, it's very weird. I only have a few loops and none are based on random variables...

    Thanks for the help, guys
     
  5. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Actually, nevermind. The issue only appears to occur when I add objects or change components? I tested it by clicking play like 10 times in a row and had no issue, added a new object, attached a few scripts, added a collider. Saved it, it froze. Ended process, started it up again, no freeze on play??
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You should do what @Brathnann said, and attach a debugger. But instead of adding a breakpoint, you should hit the "pause" button.

    It's a bit annoying, because you have to search through all of Unity's theads for the important one, but you'll probably find one of them being paused at an infinite loop somewhere in either your code, or in some package you have.
     
    Mashimaro7 likes this.
  7. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    I can't see it being caused by a loop, I only have a couple loops, all are in my starting scene, I hit play earlier today without touching any scripts like 10 times, played just fine. Added a creature, attached a few scripts, none of which had any loops in them, saved, pressed play and it froze. Loaded, pressed play again, no freeze. With the creature i added still there, no other changes. I'm so confused as to what's causing it lol, I don't even have any random.Range lines anywhere in my game.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    It's difficult to help, since we don't know your code, we can only tell you possible ways to solve similar issues based on what the reason for freezes has been in the past.

    With that in mind, it's any situation where your code enters a long running process. The reason loops are usually the culprit is because we've all been there. Any experienced programmer has hit an infinite loop and knows that Unity freezing on play is generally because of this.

    Now, in your case, it may not be an infinite loop. Maybe your computer is just old or Unity is having an off day. There isn't an easy way for us to know.
     
    Mashimaro7 likes this.
  9. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Oh, sorry, I didn't mean to sound argumentative or anything. I appreciate the help.
    The only possible cause of the issue, if it is a loop causing it, is the foreach loop in this script, because it's the most recent one I added,
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HeartMng : MonoBehaviour
    7. {
    8.     private int maxHeartAmount;
    9.     private int startHearts;
    10.     private int curMaxHealth;
    11.     private int healthPerHeart = 2;
    12.  
    13.     public Image[] heartImages;
    14.     public Sprite[] heartSprites;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         startHearts = Player.startHealth;
    20.         maxHeartAmount = Player.maxHealthEver;
    21.         curMaxHealth = Player.startHealth * healthPerHeart;
    22.         Player.playerHealth = Player.playerHealth * healthPerHeart;
    23.         CheckHearts();
    24.         UpdateHearts();
    25.     }
    26.  
    27.     public void CheckHearts()
    28.     {
    29.         for (int i = 0; i < maxHeartAmount; i++)
    30.         {
    31.             if(startHearts <= i)
    32.             {
    33.                 heartImages[i].enabled = false;
    34.             }
    35.             else
    36.             {
    37.                 heartImages[i].enabled = true;
    38.             }
    39.         }
    40.     }
    41.     void UpdateHearts()
    42.     {
    43.         bool empty = false;
    44.         int i = 0;
    45.  
    46.         foreach (Image image in heartImages)
    47.         {
    48.             if (empty)
    49.             {
    50.                 image.sprite = heartSprites[0];
    51.             }
    52.             else
    53.             {
    54.                 i++;
    55.                 if(Player.playerHealth >= i * healthPerHeart)
    56.                 {
    57.                     image.sprite = heartSprites[heartSprites.Length - 1];
    58.                 }
    59.                 else
    60.                 {
    61.                     int currentHeartHealth = (int)(healthPerHeart - (healthPerHeart * i - Player.playerHealth));
    62.                     int healthPerImage = healthPerHeart / (heartSprites.Length - 1);
    63.                     int imageIndex = currentHeartHealth / healthPerImage;
    64.                     image.sprite = heartSprites[imageIndex];
    65.                     empty = true;
    66.                 }
    67.             }
    68.         }
    69.     }
    70.  
    71.     public void TakeDamage(int dmg)
    72.     {
    73.         Player.playerHealth =  Player.playerHealth + dmg;
    74.         Player.playerHealth = Mathf.Clamp(Player.playerHealth, 0, startHearts * healthPerHeart);
    75.         UpdateHearts();
    76.     }
    77.     public void AddMaxHealth()
    78.     {
    79.         startHearts++;
    80.         startHearts = Mathf.Clamp(startHearts, 0, maxHeartAmount);
    81.  
    82.         Player.playerHealth = startHearts * healthPerHeart;
    83.         curMaxHealth = maxHeartAmount * healthPerHeart;
    84.  
    85.         UpdateHearts();
    86.         CheckHearts();
    87.     }
    88. }
    But, there's not random variables, so I don't know if this would cause it.
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    foreaches should generally not hang things, unless what you're foreaching over is an infinite/very large set.

    I'd still try to attach a debugger, pause, and see what the editor is doing.
     
    Mashimaro7 likes this.
  11. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Oh, sorry, how do you add a breakpoint? I should've asked this early, my bad haha.
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    None of your functions return anything, so the first thing to try is to put a simple
    return;
    at the first line of each of those functions, then retry your test.

    If it still locks up, the problem isn't even in the above script, look somewhere else.

    If it no longer locks up, then do the return trick on each individual function.

    Isolate, separate, break it down, keep breaking it down until you find the one line that causes your problem.

    I am going to predict that the problem is NOT in the above scripts but I'm happy to be proven wrong.
     
    Mashimaro7, Yoreki and Joe-Censored like this.
  13. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry, I didn't notice this response. The issue is, it only seems to freeze when I change too much, I wonder if it has to do with the Github addon? I heard it has issues with causing freezing. That's the only other thing I added lately... It's strange because I can test it 10 times in a row without it freezing, then get back to work, add a few new things, and suddenly it freezes.

    I literally just tested it right now, pressed play 10 times with no issue. Changed a couple minor things(no scripts, just overrided a prefab, deleted an asset) and it froze... Do you have any idea how to disable the github addon?

    Edit: Also, I had the above script entirely "return;"ed out~ lol
     
  14. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Dont wonder. Uninstall that addon and repeat the test.
     
    Mashimaro7 and Joe-Censored like this.
  15. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry, I'm not exactly sure how to uninstall it haha
     
  16. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    How did you install it? It's either in the package manager (under window/package manager), in which case you remove it from there. Otherwise you just dumped it into your assets folder somewhere, in case you can just delete the folder.

    Also make sure that it's not popping up a window somewhere. It might be that the github plugin is opening up a credentials window in order to fetch in the background, and that the entire application hangs waiting for that.

    Or something like that. People who embed git into things tend to be very sure that you want your computer to mainly spend it's time running their git plugin.
     
    Mashimaro7 likes this.
  17. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    I downloaded off the Github website, but it seems it was in a plugin folder. I deleted it and so far no freezes. I couldn't see any popups, or anything in the taskbar.

    But thanks, I guess that was the issue?
     
  18. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    How are we supposed to answer that better than you can? :p
    But i'm glad your issue seems resolved for now!
     
    Mashimaro7 likes this.
  19. evanfell2005

    evanfell2005

    Joined:
    Nov 2, 2019
    Posts:
    3
    I almost never comment on posts but I had this issue and uninstalling GitHub for Unity fixed my problem. Thank so much!
     
    Angryman_80 and Mashimaro7 like this.
  20. Angryman_80

    Angryman_80

    Joined:
    Apr 12, 2020
    Posts:
    1
    Deleting the GitHub plugin folder seemed to do the trick for me. I use another desktop client anyways. Very frustrating, I went to the package manager to uninstall GitHub it wasn't registered but the GitHub files where still in my project. Makes since that this would be the problem as Unity only froze when I made changes. I deleted the Assets/Plugins/GitHub folder. I hope this helps someone else.
     
    Mashimaro7 likes this.
  21. Immersive_Display_Solutions

    Immersive_Display_Solutions

    Joined:
    Sep 22, 2020
    Posts:
    1
    Like Angryman_80 suggested, deleting the Assets/Plugins/GitHub folder fixed the issue.
     
  22. JasonBSteele

    JasonBSteele

    Joined:
    May 31, 2016
    Posts:
    9
    I can confirm that after adding the GitHub for Unity package to Unity 2020.3, this is also happening for me.

    Looking at the commit history at https://github.com/github-for-unity/Unity, it looks like this project is dead.

    UPDATE: It seems to be called by the plugin not being logged in. Go to the GitHub Window and login (I had to use the option to use a browser). Once I did that the plugin started working well and I no longer got the hanging issue reported in the OP.
     
    Last edited: Jul 23, 2021
  23. Iman_col

    Iman_col

    Joined:
    Mar 24, 2018
    Posts:
    27
    Recientemente me sucedio algo similar. Detengo Play y Unity se queda esperando que termine la Escena. Lo que hice fue eliminar cada Script (Sin guardar la escena) y ver cual era el causante. Entontre un script que recientemente estaba editando y encontre que usaba pendingCallbacks en la clase OnApplicationQuit se habia mantenido en true y jamas devolvia false porque habia deshabilitado con comentario la iteracion.