Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity crashes when Play is pressed

Discussion in 'Scripting' started by smittywerbenjagermanjesnen, Oct 24, 2020.

  1. smittywerbenjagermanjesnen

    smittywerbenjagermanjesnen

    Joined:
    Aug 30, 2020
    Posts:
    20
    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
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    "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.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,041
    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!
     
    Last edited: Oct 25, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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. :)
     
  5. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    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.
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,041
    Haha no you got it in reverse. I meant "hard to fix", not hard to do :)

    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.
     
  7. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,464
    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.
     
  8. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Brother, I feel your pain.
     
    orionsyndrome likes this.
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,041
    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.