- Home /
How can I get the current location using Input.Location from the background worker?
if (_backgroundWorker != null) _backgroundWorker.CancelAsync();
_backgroundWorker = new BackgroundWorker();
_backgroundWorker.DoWork += (o, a) =>
{
// executed on background thread
// do slow running computationaly intense work, check periodically
// if (a.IsCanceled) return;
// assign result as
// a.Result = a.Argument+"!";
while (true)
{
//Here I want to get the location
//Below two lines give me the error like get_lastData can only be called from the main thread.
//Constructors and field initializers will be executed from the loading thread when loading a scene.
startLat = Input.location.lastData.latitude;
startLng = Input.location.lastData.longitude;
Thread.Sleep(5000);
}
};
_backgroundWorker.RunWorkerCompleted += (o, a) =>
{
// executed on main thread
// you can use a.Result
};
_backgroundWorker.RunWorkerAsync("A1");
if (_backgroundWorker != null) _backgroundWorker.Update();
**Trying to get the current latitude and longitude from the background worker but it gives me error like : get_lastData can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene.
I am new to Unity so please suggest some ideas to get current location when the app is not in foreground. **
Your answer
![](https://koobas.hobune.stream/wayback/20220612101144im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is There A Way To Use GPS Data On A Mac Stand Alone build? 1 Answer
Interrupting a long process: progress watching? 1 Answer
Input.compass & Input.location for Smart-glasses(non-handheld devices) issue. 1 Answer
How to import GPS location coordinates from Android device? 4 Answers
Can unity access location information from an external source? 0 Answers