Unable to pass values from one script to another
Script GetGPS
gets user coordinates and GetAqiInfo
gets AQI information in users current location using weatherbit.io API. I have tested and found GetGPS script works fine (lat/long vales are correct), but when I try to pass those values to GetAqi script, it automatically puts lat: 0 long: 0. Where am I going wrong?
GetGPS Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.Networking;
public class GetGPS : MonoBehaviour
{
public static float latitude;
public static float longitude;
private void Start()
{
DontDestroyOnLoad(gameObject);
StartCoroutine(StartLocationService());
}
private void Awake()
{
if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
{
Permission.RequestUserPermission(Permission.FineLocation);
}
}
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 determine device location");
yield break;
}
latitude = Input.location.lastData.latitude;
longitude = Input.location.lastData.longitude;
yield break;
}
}
GetAqi Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using SimpleJSON;
public class GetAqi : MonoBehaviour
{
public static string latitude;
public static string longitude;
public static string cityName;
public static double currentAqi;
private readonly string baseWeatherbitURL =
"https://api.weatherbit.io/v2.0/current/airquality?";
private readonly string key = "*********************";
// Start is called before the first frame update
void Start()
{
latitude = GetGPS.latitude.ToString();
longitude = GetGPS.longitude.ToString();
StartCoroutine(GetAqiInfo());
}
private IEnumerator GetAqiInfo()
{
string weatherbitURL = baseWeatherbitURL + "lat=" + latitude +
"&lon=" + longitude + "&key=" + key;
UnityWebRequest aqiInfoRequest = UnityWebRequest.Get(weatherbitURL);
yield return aqiInfoRequest.SendWebRequest();
//error
if (aqiInfoRequest.isNetworkError || aqiInfoRequest.isHttpError)
{
Debug.LogError(aqiInfoRequest.error);
yield break;
}
JSONNode aqiInfo = JSON.Parse(aqiInfoRequest.downloadHandler.text);
cityName = aqiInfo["city_name"];
currentAqi = aqiInfo["data"][0]["aqi"].AsInt;
Debug.Log($"New data available: ${currentAqi}");
}
}
Answer by streeetwalker · Sep 06, 2020 at 10:14 AM
Hi @parthshuklaa. You are able to reference the values (you are not "passing" them).
It seems like you need to set you lat and long values before you let GetAqiInfo do anything with them.
The problem seems to be that GetAqiInfo is referencing them in its Start method before you give them values in GPS, because the coroutine in your GPS script takes its sweet time to set them.
there are probably several ways to resolve this problem. One would be to let GPS call a function in GetAqlInfo to set them at the end your GPS coroutine.