- Home /
NullReference in Start() or Awake() when testing on Android device, works fine in Editor
i'm getting a weird null reference exception when i debug my app on device (Asus Transformer Prime).
This happens in app initialization, both in Start() and Awake(). The Transform is being assigned in the Inspector, and when launching the app in the Editor it works fine, i get a reference to the Transform as expected, while on the device i get a null reference.
public Transform weirdObject;
void Awake()
{
if(this.weirdObject == null) Debug.Log("meh. null.");
else Debug.Log("yea it's not null");
}
There is zero chance of anybody deter$$anonymous$$ing what is wrong based on the code you've included. All this does is check for null. The problem has to be elsewhere.
i know the code snippet is not the problem, thanks. the problem is described in the text above the code snippet.
with it i wanted to enforce the point that this happens on app initialization, before anything complicated happens on my side, so it should be deter$$anonymous$$istic? in fact, it is, i seem to get this consistently on each launch on the device.
maybe someone here has had a similar issue. what could cause the difference in execution between the editor and the device?
I know from the unity docs that assets aren't readily available on the first level on android devices. Try making a level with nothing in it except a script that loads the level you are having trouble with. $$anonymous$$ake that level load first. Just a suggestion.
ah thanks. this doesn't solve the problem on the device, but when i start the launcher level in the editor, i now get the same null reference from my problematic level within the editor. should be easier to debug at least!
Did you solve this? I'm getting the same issue in Window; the game runs perfectly well inside the Editor, but when I make a build and run it, huge swathes (but not all) of the inspector values appear to be null.
Answer by silva_raptor · Jun 30, 2013 at 02:37 PM
I had this exact problem and I couldn't find an answer. The NullReferenceException happened only on Android and iOS, while it was working fine on the editor. The script wasn't even able to find anything using GameObject.Find(). It happened only in one of the objects in the scene, so my "dirt" solution was to create an empty gameobject with a singleton script linked to all the needed references, and use it as a "helper" of my problematic gameobject.
I think that this is some kind of strange bug of unity.
Answer by bajtek · Dec 20, 2017 at 07:41 PM
Hi, It's a topic from long past but I had the same problem and I found solution for it in my case. I used SQLite and I added one table to database. When I copied .db file from Assets to StreaminAssets directory it still didn't work. The solution for it was changing version of build in Edit>Project Settings>Player from 1.0 to 1.1. I don't know why but it works.
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 3 Answers
Unity IAP Button: NullReferenceException: Object reference not set to an instance of an object 1 Answer
Script works on some objects and not others, also varies between scenes... 0 Answers
Building for android freezes unity editor 2 Answers
Unity3D Android NullReferenceException for class properties during using Input.location.lastData 2 Answers