Search Unity

Console error when script is unchecked

Discussion in 'Scripting' started by ericbintner, Aug 15, 2019.

  1. ericbintner

    ericbintner

    Joined:
    Jul 22, 2019
    Posts:
    26
    I've noticed when there's an error in a script and I uncheck the script in Unity, the error still persists, like it's still running the code. I thought uncheck items in the inspector were ignored. Please help me understand this.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It's not no much that they're ignored, but most of Unity's methods (Start, Update, OnEnable, etc) don't run. It's possible your error is somewhere else that may still be getting run (e.g. in a method called by another script).

    Or, more likely, you have a second copy of the script inadvertently added to some other object in your scene, and that one is still running.

    If you're still confused, we'd need to see the error and the code that causes it to provide any additional insight.
     
  3. ericbintner

    ericbintner

    Joined:
    Jul 22, 2019
    Posts:
    26
    If I remove the script it no longer causes the error, that's how I know it's still causing the error even when it's unchecked. It's really weird because it causes the error when I'm not even running the app. It was on update() -- and I expected checking the box to behave like commenting the block of code. I'll just comment the whole block of code or remove it entirely since I don't really understand what the checkmark does with pages of code.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It sounds like you are getting confused by the difference between compiler errors and runtime errors.

    A runtime error is a problem that only gets noticed when your application is running, usually because it depends on the specific values of the variables. For instance, x = 1 / y is perfectly legal code unless y is zero, in which case you'll get an error at run-time. Similarly, myObject.someMember is perfectly legal code unless myObject is null, in which case you'll get an error at run-time.

    A compiler error occurs when you can tell the code is wrong without even trying to run it, usually because the syntax is illegal. x = */ y is always illegal no matter what the values of x and y might be.

    Unity compiles your code every time the editor has focus and it notices that your code has changed (i.e. when you switch back to the main Unity window after saving your code). Unity always compiles all of your code, whether it's attached to anything in the scene or not. (This is important, because certain kinds of code don't need to be attached to any GameObject in your scene in order to have an effect.)

    If there is a compiler error anywhere in your entire project, then Unity can't finish compiling, and therefore cannot make a new runable version of your game. You always need to fix compiler errors before you can continue, even if that code is never going to run (the computer usually can't figure out at compile-time whether the code is ultimately going to run or not).

    On the other hand, when Unity gets a runtime error, it will stop executing that particular function call, but it will still try to continue running your game.

    Unchecking the box on some component on one of your game objects can't work the same as commenting out the code, for many reasons:
    • The same code might also be attached to a different object somewhere in your scene (or even in a different scene! Code is only compiled once for all scenes)
    • That box can be checked and unchecked while the game is running
    • Commenting out that class might break other parts of your code that refer to it. For instance, if class Foo contains a public variable of type Bar, then "commenting out" the Bar class means that Foo won't compile anymore. You don't want to do that just because you have temporarily stopped Bar from running.
    • If your program is big, compiling it can take a fair bit of time; you wouldn't want to wait for it every single time a box is checked or unchecked
    • In the general case, it might not actually be possible to figure out which code is supposed to be "commented out". A single file can contain multiple classes (or structs, enums, etc.), and if the file has a mistake that prevents it from compiling, you might not be able to figure out which parts of the code are "supposed to be" part of the "commented out" class.

    What that checkmark actually does to your code is: absolutely nothing! Your code hasn't changed. What changes is essentially that the Unity engine will stop trying to automatically call certain functions in your code.
     
    ericbintner and StarManta like this.
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    ^ And that right there is why you should post the actual error. Seeing that it was a compile error and not a runtime would have gotten you a useful response immediately, rather than 3 messages later. (and we probably could've fixed your compile error too while we were at it)
     
    ericbintner and Deleted User like this.
  6. ericbintner

    ericbintner

    Joined:
    Jul 22, 2019
    Posts:
    26
    Ah, yes. This all makes sense. My confusion is all because I'm learning how Unity interacts with its underlying codebase, I'm not used to a GUI for code. I get how it compares to a node.js front-end dev build, so your reply makes perfect sense. Great explanation!