Lots of Exceptions on Executable but none in Editor
Hello,
I'm having a problem here and I'm truly clueless about how to solve it. My game runs just fine, no exceptions, when I hit the play button in the editor. However, once I run it's executable the game throws a great amount of exceptions. Most of them are NullReferenceExceptions.
I tried to clean the project in Visual Studio, compile it again, replaced some ProjectSetting files for new ones of new projects, but still I can't figure why so many exceptions are being throw.
Is there any difference between the build and the editor version of a project? What are the common causes to this?
Thanks in advance. Any kind of help is welcome!
from my experience changed execution orders could be the cause. for example two scripts doing something in their Start methods where one relies on the other to execute first, it's possible, since Start calls are arbitrarily ordered, that they switch places.
second, a lot of errors are usually just happening because some init did not run properly and the errors are thrown from Updates.
Check what's Null and see when and where that's initialized
$$anonymous$$anaged exceptions always comes with a stacktrace. So you know the exact method and line number where it happens. Checking this line to figuring out what might be the cause for the null reference is part of a process we call debugging. However we can't debug your problems because:
we don't have your code or project
we don't know your stacktrace
Note that when you're debugging focus on the first error. Any subsequent errors might be a result of the first one. If an initialization procedure is aborted by an exception, everything that should have been initialized after the point where the error occured is not executed. This may affect countless of see$$anonymous$$gly unrelated errors.
If you want any further help on your issue you have to include more information on your problem
Since this is not a question about Unity but a help request with a debugging problem i will move this question into the help room.
Answer by tormentoarmagedoom · Jun 26, 2018 at 10:22 AM
Good day.
I had some similar issue some time ago (other Unity version but maybe is still the same)
The order of execution of all Awake methods was different in editor than in Build executable. The same with all On Enable, and Starts. (for example the Start method of ScriptA was executed before Start of ScriptB, but in Build, the Start of scriptB was executed before Start of scriptA)
So when some script gives information to another script, some objects was execturing it Start method before other object Start, causing some variables was not yet defined and giving the error.
I solved it making a clear order of definition of variables and assigning values using correctly Awake Onenable and Start.
What you can be sure is that all Awakes will be before all others methods, then all OnEnable, and then all Start. And finally the Updates.
Unity have also a way to configure what scripts will be executed before/after what other scripts, so you can configure it at your own.
Edit > Project Settings > Script Execution Order
Bye!