- Home /
Unity3D Android NullReferenceException for class properties during using Input.location.lastData
I found a very strange bug in Android build from Unity3D.
using UnityEngine;
public class Test : MonoBehaviour {
public bool inited;
void Start () {
this.inited = false; // OK!
}
void Update()
{
this.inited = true; // OK!
}
}
Yes... It's simple and works :)
BUT! If I read any data from Input.location.lastData like:
using UnityEngine;
public class Test : MonoBehaviour {
public bool inited;
void Start () {
this.inited = false; // OK!
}
void Update()
{
if (this.inited && Input.location.status == LocationServiceStatus.Running)
{
float l = Input.location.lastData.latitude;
Debug.Log("TEST [l] = " + l); // OK. Data present
Debug.Log("TEST [this.inited] = " + this.inited); // !ERROR! NullReferenceException
}
}
}
then links to the class properties disappear. Any ideas?
Just tested. Boolean "this.inited" triggers NullReferenceException.
Answer by OperationDogBird · Sep 14, 2013 at 07:42 PM
A bool cannot be null, thus you cannot get a null reference exception.
I also do not see a reason to debug that var from within the condition. You already know that it is true.
Answer by Webjema · Sep 15, 2013 at 06:52 AM
This code is for example only. Real code is more complex. In real code I tried write GPS data to inited (in Start) List var. If I write something to List BEFORE
float latitudeValue = Input.location.lastData.latitude;
no problem. If I try write latitudeValue (it's AFTER accessing to Input.location.lastData) to the List var
NullReferenceException for List type class property.
Your answer
Follow this Question
Related Questions
GPS for Unity Android 1 Answer
Android Tablet GPS not working 0 Answers
GPS Support on Android with Unity3d 2 Answers
Unity3D Android 8.1 LocationServiceStatus GPS 0 Answers
Keep unity running when minimized on iOS and Android 2 Answers