Trying to call a stored variable from one file.cs into another file2.cs
I have found a script to find the GPS location of the user, here's that code:
using UnityEngine;
using System.Collections;
public class CurrentLocation : MonoBehaviour
{
public static string CurrLoc { get; internal set; }
IEnumerator Start()
{
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
yield break;
// Start service before querying location
Input.location.Start();
// 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)
{
print("Timed out");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
print("Unable to determine device location");
yield break;
}
else
{
// Access granted and location value could be retrieved
string CurrLoc = Input.location.lastData.latitude + " " + Input.location.lastData.longitude;
}
// Stop service if there is no need to query location updates continuously
// Input.location.Stop();
// Line above commented out because it needs to constantly update.
}
}
I want to call CurrLoc from CurrentLocation.cs (the code above) into another Menu.cs file into a function, here's that code:
public void SmsButton()
{
string Content = "sms:" + ContNum + "?body=" + WWW.UnEscapeURL(CurrentLocation.CurrLoc);
Application.OpenURL(Content);
}
Basically what this will do is actively get the GPS location of the user and the button that calls "SmsButton()" will text the location to a specified contact number. While this does not give any complier errors it doesn't return any values. How can I accurately call the CurrLoc variable from the CurrentLocation.cs?
Answer by btmedia14 · Aug 25, 2016 at 12:48 PM
Within the Start() method of class CurrentLocation change the line
string CurrLoc = Input.location.lastData.latitude + " " + Input.location.lastData.longitude;
to
CurrLoc = Input.location.lastData.latitude + " " + Input.location.lastData.longitude;
The CurrLoc variable is being redeclared as a local variable inside the Start() method. The public static version does not get assigned and will return the default value, typically a null string.
I see the suggestions, and made the change. It still does not input the location into the body of the text.
@aarontbarksdale $$anonymous$$ay I suggest adding some Debug statements to view the value of CurrLoc when it is set. If you are able to step through those lines in Debugger may be easier. Wrap some Debug logs similar to:
else
{
Debug.Log("Location status: " + Input.location.status);
Debug.Log("lastData: " + Input.location.lastData);
CurrLoc = Input.location.lastData.latitude + " " + Input.location.lastData.longitude;
Debug.Log("CurrLoc: " + CurrLoc);
}
This will at least indicate what the device location services are returning and what gets saved in CurrLoc. The susbsequent call from SmsButton() appears fine, but could debug that also. Fyi: The Debug.Log output will display in the Unity console window
@btmedia14 I don't know why I didn't think to check the Log, however, I'm trying to run this off of an android phone and not the computer. Every time I hit the button on my phone it doesn't show in the log. But when I do it on my computer, even with Location Services on, it doesn't return any values. It says:
Location status: Stopped UnityEngine.Debug:Log(Object) $$anonymous$$enuScript:SmsButton() (at Assets/$$anonymous$$enuScript.cs:23) UnityEngine.EventSystems.EventSystem:Update()
lastData: UnityEngine.LocationInfo UnityEngine.Debug:Log(Object) $$anonymous$$enuScript:SmsButton() (at Assets/$$anonymous$$enuScript.cs:24) UnityEngine.EventSystems.EventSystem:Update()
CurrLoc: 0 0 UnityEngine.Debug:Log(Object) $$anonymous$$enuScript:SmsButton() (at Assets/$$anonymous$$enuScript.cs:26) UnityEngine.EventSystems.EventSystem:Update()
@aarontbarksdale Note, there is a bit of a time delay, sometimes several seconds, between when the location services are started, initialised and when the data is available. Again, stepping through debugger, or placing appropriate Debug.Log statements will allow to deter$$anonymous$$e where in the steps the issue is raised.
Also, recall the location is only returned for mobile devices, and not for location from desktop platform, even if desktop has location settings enabled.
Your answer
Follow this Question
Related Questions
Flip 3D Character Rotation 180 on Y axis 1 Answer
How to fix snake behavior in Unity3d? 0 Answers
How can I make this character move smoothly? 1 Answer
MobController 1 Answer
Unity3d Character Spawn Point 0 Answers