Unity Build nullReference, but editor sees through it!
Hi there, sorry to bother but this kind of errors are new to me because I've been coding mainly inside the editor!
It seems that 1 line in my Awake() returns != null when in editor, and == null once built.
This is the line, though it doesn't help too much per se:
//Outputting
textBehaviour.LinesToPrint(myDialogue.NpcLines[0]);
textBehaviour is an instance of one of my Classes, it just has to print a string.
I believe that, since the development build says nullReference and not Index out of Range, the problem is that instance itself.
Could it be that myDialogue has something to do with this too? That maybe the error derives from a null parameter?
In the editor I did tweaked scripts execution order to fit my needs... maybe in builds that's a bad thing?
I'm trying to build for mac. I know I've provided scarce infos on this, but I don't know what to look for... I could really use some help.
I've looked previous similar questions but I can't understand how it's possible that the same variable works if in editor and screws up if in build!
Thanks :D
Answer by Statement · Nov 10, 2015 at 08:53 PM
What is null?
textBehaviour or myDialogue?
If you can't tell, figure it out. Attach a debugger or call print or Debug.Log/Warning/Error.
void Awake()
{
LogNullError(textBehaviour, "textBehaviour");
LogNullError(myDialogue, "myDialogue");
textBehaviour.LinesToPrint(myDialogue.NpcLines[0]);
}
void LogNullError(object o, string name)
{
if (o == null)
Debug.LogErrorFormat(this, "The variable {0} is null", name);
}
Then figure out where that object is set. Maybe it's a result from a file load? Maybe that part behave different depending on the location of your executable or operating system? And so on.
Thanks a lot! I'll use those calls and check it out.
Yes, there's a bunch of stuff working to provide that line; the current class asks textBehaviour to grab a string from another class instance.
Since serialization is involved I believe the error could derive from there... although I made sure to use strings like Application.persistentDataPath to avoid wrong path issues...
Now that I've tested it, it says that the third instance is ok but fails to compile and return the needed string.
That is: textBehaviour is ok, myDialogue is ok, myDialogue.NpcLines[0] doesn't exist ( yet).
Since I know it exists because the editor debugs properly, what should I do to make sure my scripts run in the proper order also in the build?
Thanks a lot for helping me!
Just follow the code to where myDialogue.NpcLines is assigned, or where NpcLines[0] is assigned, if that is what is null. Trail the flow of the program backward and inspect key code junctions to see what caused it to be null. (Was it assigned null, or was it never assigned anything? Why? And so you iterate like that to figure out the source. It makes no sense to stop "half way" between an assignment and the failure, just to be clear. It only makes sense to test the locations where a value is assigned (or test if that code was never run in the first place))
I've managed to solve the initial problem but now I've found out that the build can't serialize files to mac directories.
I'll see how that goes and if needed I'll open a new question!
By the way, it wasn't about script orders, it was about a static reference that in build runtime was not as trustworthy as it seemed in editor.
Thanks again!
Your answer
Follow this Question
Related Questions
NullReferenceException after build 1 Answer
Can't return KeyCode in build 0 Answers
CommandInvokationFailure: Failed to re-package resources. Tried various remedies Please help... 0 Answers
compareTag function not working in build version 0 Answers
Can't "build and run". Can only use the "build "option 0 Answers