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
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.
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.
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…
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??
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.
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.
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.
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,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HeartMng : MonoBehaviour
{
private int maxHeartAmount;
private int startHearts;
private int curMaxHealth;
private int healthPerHeart = 2;
public Image[] heartImages;
public Sprite[] heartSprites;
void Start()
{
startHearts = Player.startHealth;
maxHeartAmount = Player.maxHealthEver;
curMaxHealth = Player.startHealth * healthPerHeart;
Player.playerHealth = Player.playerHealth * healthPerHeart;
CheckHearts();
UpdateHearts();
}
public void CheckHearts()
{
for (int i = 0; i < maxHeartAmount; i++)
{
if(startHearts <= i)
{
heartImages[i].enabled = false;
}
else
{
heartImages[i].enabled = true;
}
}
}
void UpdateHearts()
{
bool empty = false;
int i = 0;
foreach (Image image in heartImages)
{
if (empty)
{
image.sprite = heartSprites[0];
}
else
{
i++;
if(Player.playerHealth >= i * healthPerHeart)
{
image.sprite = heartSprites[heartSprites.Length - 1];
}
else
{
int currentHeartHealth = (int)(healthPerHeart - (healthPerHeart * i - Player.playerHealth));
int healthPerImage = healthPerHeart / (heartSprites.Length - 1);
int imageIndex = currentHeartHealth / healthPerImage;
image.sprite = heartSprites[imageIndex];
empty = true;
}
}
}
}
public void TakeDamage(int dmg)
{
Player.playerHealth = Player.playerHealth + dmg;
Player.playerHealth = Mathf.Clamp(Player.playerHealth, 0, startHearts * healthPerHeart);
UpdateHearts();
}
public void AddMaxHealth()
{
startHearts++;
startHearts = Mathf.Clamp(startHearts, 0, maxHeartAmount);
Player.playerHealth = startHearts * healthPerHeart;
curMaxHealth = maxHeartAmount * healthPerHeart;
UpdateHearts();
CheckHearts();
}
}
But, there’s not random variables, so I don’t know if this would cause it.
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.
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
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.
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.
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.