Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 parthshuklaa · Sep 05, 2020 at 07:07 PM · scripting problemscripting beginnerscriptingbasics

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}");
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
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

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

328 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 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 disable raycast/groundchecker on OnTriggerEnter? 0 Answers

Displaying a different sprite depending on which angle a character is viewed from 1 Answer

UI Text Not Updating 1 Answer

When I change a bool in a script on one object it doesn't change the bool on the same script on another object? 1 Answer

Get() and Set() values of a gridArray from another scipt 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