Unity5 and Unity Remote 5 will not work with GPS on my Android lolipop 6.01 phone

So I have a Android Lollipop 6.0.1. I have gone through the tutorials to make it work with accelerometer, gyro and compass. All of which work. However it always seems to think that the GPS on my phone is not enabled even though it is.

This is the same after I compile it it and install it on the phone and if a try to run it through Unity Remote 5.

Am I missing an extra setting? My GPS Location is turned on on my phone and it works with other phone apps. Just not the Unity ones that I’m trying to create. I’m just trying to get simple latitude and longitude data

The code I am using is below. I only get to the point in the code !Input.location.isEnabledByUser and it kicks me out with the Debug.Log “user has not enabled gps”

Thanks All, hope you can help.

using UnityEngine;
using System.Collections;
using System;

public class GPS : MonoBehaviour {
    public static GPS Instance { set; get; }
    public float latitude;
    public float longitude;

    private void Start()
    {
        
        Instance = this;
        DontDestroyOnLoad(gameObject);
        StartCoroutine(StartLocationService());

    }

    private IEnumerator StartLocationService()
    {
        
        if (!Input.location.isEnabledByUser)
        {
            Debug.Log("user has not enabled gps");
            yield break;
        }

        Input.location.Start();
        int maxWait = 20;
        while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        if (maxWait <= 0)
        {
            Debug.Log("Timed Out");
            yield break;
        }

        if(Input.location.status == LocationServiceStatus.Failed)
        {
            Debug.Log("Unable to dtermine device location");
            yield break;
        }


        latitude = Input.location.lastData.latitude;
        longitude = Input.location.lastData.longitude;
        yield break;
    }
}

I had exactly the same problem. The solution which worked for me was to add some delay into StartLocationService(). I think the problem is that Unity Remote and Unity Editor require some time to establish a connection. If you try to start the location service before the connection between remote and editor is established you will fail. This will happen if you try to start location service in the start().

See my solution below:

private IEnumerator StartLocationService(float desiredAccuracyInMeters, float updateDistanceInMeters)
    {
        // Wait until the editor and unity remote are connected before starting a location service
        if (isUnityRemote)
        {
            yield return new WaitForSeconds(5);
        }

        // First, check if user has location service enabled
        if (!Input.location.isEnabledByUser)
        {
            Debug.Log("No locations enabled in the device");
            yield break;
        }

        // Start service before querying location
        Input.location.Start();

        if (isUnityRemote)
        {
            yield return new WaitForSeconds(5);
        }

        // Wait until service initializes
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        // Service didn't initialize in 20 seconds
        if (maxWait < 1)
        {
            Debug.Log("Service didn't initialize in 20 seconds");
            yield break;
        }

        // Connection has failed
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            Debug.Log("Unable to determine device location");
            yield break;
        }
        // Access granted and location value could be retrieved
        else
        {
            float lat = Input.location.lastData.latitude;
            float lon = Input.location.lastData.longitude;

        }
    }

I added 2 seconds delay in the beginning of the StartLocationService() and directly after the Input.location.Start(). I also made a global boolean isUnityRemote to be able to disable these delays if I want. You could also use “Input.location.status == LocationServiceStatus.Running” in the while loop instead of this if you want to be more elegant.