Not able to retrieve information from script in unity.
I am a newbie to Unity, and have been trying to access weatherbit.io API to get some information using coordinates of the user. For that I have used input.location() service, that works flawlessly. But whenever I try to parse that to a new script to utilize the API, the script does not print anything. I have attached the GetAqiInfo script, the UpdateAqi script as well as a screenshot of the application running on android.
Thank you so much in advance.
GetAqiInfo script:
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GetAqiInfo : MonoBehaviour
{
public static GetAqiInfo Instance { set; get; }
private float timer;
public float minutesBetweenUpdate;
public AqiInfo Info;
private float latitude;
private float longitude;
private bool locationInitialized;
public GPS gpslocation;
public Text currentAqiText;
public void Begin() {
latitude = gpslocation.latitude;
longitude = gpslocation.longitude;
locationInitialized = true;
}
void Update() {
Instance = this;
if (locationInitialized) {
if (timer <= 0) {
StartCoroutine (GetAqi());
timer = minutesBetweenUpdate * 180;
} else {
timer -= Time.deltaTime;
}
}
}
private IEnumerator GetAqi()
{
var www = new
UnityWebRequest("https://api.weatherbit.io/v2.0/current/airquality?lat=" +
latitude
+ "&lon=" + longitude + "&key=___________________")
{
downloadHandler = new DownloadHandlerBuffer()
};
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
//error
yield break;
}
Info = JsonUtility.FromJson<AqiInfo>(www.downloadHandler.text);
currentAqiText.text = "Current AQI: " + Info.data.aqi;
}
}
[Serializable]
public class AqiInfo
{
public Data data;
public string city_name;
public float lon;
public string timezone;
public float lat;
public string country_code;
public string state_code;
}
[Serializable]
public class Data
{
public string mold_level;
public double aqi;
public double pm10;
public double co;
public double o3;
public string predominant_pollen_type;
public double so2;
public double pollen_level_tree;
public double pollen_level_weed;
public double no2;
public double pm25;
public double pollen_level_grass;
}
The following is the code for UpdateAqi script to print the AQI info.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UpdateAQI : MonoBehaviour
{
public Text coordinates;
private void Update()
{
coordinates = GetAqiInfo.Instance.currentAqiText;
}
}
Your answer
Follow this Question
Related Questions
Can this script be better optimized? 1 Answer
Code not working as it should 1 Answer
Help with C# code + Unity 1 Answer
is there a way to save data on Android before quit? 1 Answer
Problem with inventory asset's script and its working 0 Answers