Unity crashes when Play is pressed

Something has happened and as I removed every code that I thought could’ve caused it, it kept crashing. I don’t know what causes it. Here’s an Editor log:
https://pastebin.com/raw/NJQx7A0a

“When you have eliminated the impossible, whatever remains, however improbable, must be the truth.”

I’m no wizard at reading logs, but it appears you have a lot of GameObjects that used to have scripts on them, and the scripts are missing now (maybe because you have removed the code you thought was causing the crash).

When I have a problem like this, I solve it this way:

  1. Make a copy of the current project and make that your current project.
  2. Remove about half of everything in the current project.
  3. Start the project.
  4. Is the problem still present?
    4a (YES): If you can see what’s causing it, you have solved your problem, if not, continue by going back to Step 1.
    4b (NO): Take everything out of the current project and put back what you took out in Step 2, and continue by going back to Step 3.

Even though you have removed the code you thought could have caused your crash, something in your project is still causing a problem. If there is still code in your project, take it out, one half at a time, according to the steps above. If there are still GameObjects with components on them, take out the components, one half at a time (or even the GameObject themselves).

If, in the unlikely case that you remove absolutely everything and Unity still crashes, reinstall Unity (it may have become corrupt). I predict this will not be necessary, however.

Good luck.

The only times I was able to produce a CTD through code was by inadvertently introducing a stack overflow. This can happen when recursion goes out of hand (easy), or when you accidentally make a circular reference due to bad naming or bad logic (hard__*__). The latter usually happens as a consequence of calling a method that calls some setter which calls that method back in a not-at-all-obvious sequence of events, and so on. Soon enough the stack will overflow, and bam. Can’t speak about the other areas of Unity, this is strictly through code. Thankfully, in all cases, I was able to pinpoint the dangerous areas of the code, eliminate them temporarily, and work my way toward fixing this.

* edit: hard to fix, not hard to implement!

1 Like

For the record, I find bad logic extremely easy to write, not hard at all. I write bad logic all the time @orionsyndrome !

The key is fixing it… I hope to always get better at that. :slight_smile:

1 Like

Yeah, I can generally get a thousand lines of bad logic running by noon, on a typical day. I think he means it is hard to get Unity to crash to the desktop with bad logic. Pounding my forehead onto the desktop due to bad logic is pretty easy to get me to do, however.

1 Like

Haha no you got it in reverse. I meant “hard to fix”, not hard to do :slight_smile:

By contrast, recursion is typically easy to fix, because you 1) typically know what you’re doing if you’re doing stuff recursively, and 2) you have a clear hunch of it being the culprit.

With logic recursion, oh boy, that can be so hard to nail down, exactly because of hard crashing. And in my experience it’s always because I’m playing a smart ass with “smart” setters. Your mileage may vary.

1 Like

I agree with the suggestion by @orionsyndrome - the only time I’ve seen the editor crash every time entering Play Mode was also due to stack overflow.

Brother, I feel your pain.

1 Like

Oh well, as long as I’m able to fix it, it’s a learning process. But let me tell you, having a version control is a life saver. I don’t work for companies for quite a while now, so this doesn’t come out of box, but I tend to be very disciplined about this. Having a version that worked, and a file diff saved me from a couple of really hairy situations.