Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by omanuke · Sep 20, 2021 at 11:50 AM · location

Call input.location.Start() but status keeps to be Stopped.

I'm trying to implement getting location feature. The code below works like a charm in test project but it doesn't work in my real project. Calling Input.location.Start() doesn't bring any effect to Input.location.status. This app has permission. Original both project works in Unity 2020.3.4f1. I changed Unity version of real project to 2020.3.13 but result is same.

RequestUserPermission works fine but calling Input.location.Start() seems not work.

There is use feature entry in AndroidManifest.

I wanna know what causes these different result in two project with same code. Any info is really helpful. Regards,

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Threading.Tasks;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine.UI;
 # if PLATFORM_ANDROID
 using UnityEngine.Android;
 # endif
 public class Gyro : MonoBehaviour
 {
     private string _gpsStage = "-";
     
     GameObject dialog = null;
     
     public float latitude;
     public float longitude;
     public float altitude;
     [SerializeField] private Text _text;
     private bool _gpsStopped;
     private bool _permissionRequested;
     private void Start()
     {
         StartCoroutine(Loop());
         DontDestroyOnLoad(gameObject);
         Input.gyro.enabled = true;
         Input.compass.enabled = true;
     }
     
     public void RequestPermission()
     {
     Debug.Log("RequestPermission");    
 #if UNITY_ANDROID
         Debug.Log("RequestPermission2");    
         Permission.RequestUserPermission(Permission.CoarseLocation);
         _permissionRequested = true;
 #endif
     }
     void OnApplicationPause(bool pauseStatus)
     {
         Debug.Log($"OnApplicationPause ={pauseStatus}");
         // check flag
         if (!pauseStatus && _permissionRequested)
         {
 #if UNITY_ANDROID
             // has permission?
             if (Permission.HasUserAuthorizedPermission(Permission.CoarseLocation))
             {
                 Debug.Log("got permission");
                 //do something if got permission
                 StartCoroutine(StartLocationService(0));
                 _permissionRequested = false;
             }
 #endif
         }
     }
     
     public void StartLocation()
     {
         Debug.Log("");
         _gpsStopped = false;
         _gpsStage = "";
         latitude = float.NaN;
         longitude = float.NaN;
         StartCoroutine(StartLocationService(0));
     }
 
     public void StopLocation()
     {
         _gpsStopped = true;
         if(Input.location.status==LocationServiceStatus.Initializing||
            Input.location.status==LocationServiceStatus.Running)
             Input.location.Stop();
     }
 
     IEnumerator Loop()
     {
         while (true)
         {
             yield return new WaitForSeconds(0.1f);
             var nl = System.Environment.NewLine;
             _text.text = $"{DateTime.Now} isEByUser={Input.location.isEnabledByUser} status={Input.location.status} {nl}"+
 #if UNITY_ANDROID
                          $"hasPermission={Permission.HasUserAuthorizedPermission(Permission.CoarseLocation)}{nl}"+
 #endif
                          $"stage={_gpsStage} gpsStopped={_gpsStopped}{nl}la={latitude:F3} lo={longitude:F3} al={altitude:F3}{nl}"+
                          $"q={Input.gyro.attitude.eulerAngles}{nl}deg={Input.compass.magneticHeading:F2}{nl}"+
                         $"tH={Input.compass.trueHeading}{nl}"+
                          $"rawVec={Input.compass.rawVector}{nl}";
         }
     }
 
     private IEnumerator StartLocationService(float wait)
     {
         yield return new WaitForSeconds(wait);
         _gpsStage = "starting...";
         yield return new WaitForSeconds(0.5f);
         // First, check if user has location service enabled
         if (!Input.location.isEnabledByUser)
         {
             Debug.Log("GPS not enabled");
             _gpsStage = "not enabled";
             yield break;
         }
 
         _gpsStage = "start";
         // Start service before querying location
         Input.location.Start();
         _gpsStage = "waiting not stopped";
         //this is needed to make location service to work fine after requesting permission.
         while (Input.location.status == LocationServiceStatus.Stopped)
         {
             yield return new WaitForSeconds(1);
             Debug.Log("...waiting");
             Input.location.Start();
             //Debug.Log("call Start again---");
         }
         // Wait until service initializes
         int maxWait = 20;
         while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
         {
             yield return new WaitForSeconds(1);
             maxWait--;
         }
         _gpsStage = $"stopped changed to {Input.location.status}";
         // Set locational infomations
         while (!_gpsStopped) {
             if (Input.location.status == LocationServiceStatus.Running)
             {
                 latitude = Input.location.lastData.latitude;
                 longitude = Input.location.lastData.longitude;
                 altitude = Input.location.lastData.altitude;
             }
             yield return new WaitForSeconds(5);
         }
         _gpsStage = "stopped";
     }
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image omanuke · Sep 20, 2021 at 07:46 AM 0
Share

I deleted almost script, asset, package and so on except test scene and test script. It works. I don't know what causes this strange behaviour. I'll try to delete one by one and detect what causes it.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change a public location,how to get a public variable 1 Answer

how to access “device only” gps in Unity 0 Answers

Warning:Trying to move asset to location it came from...[Please Help] 0 Answers

iOS Location Permission 1 Answer

Trying to Build GPS Tracking Based App, Help 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges