Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 aarontbarksdale · Aug 25, 2016 at 11:34 AM · c#unity 5

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?

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 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.

Comment
Add comment · Show 9 · 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
avatar image aarontbarksdale · Aug 25, 2016 at 01:21 PM 0
Share

I see the suggestions, and made the change. It still does not input the location into the body of the text.

avatar image btmedia14 aarontbarksdale · Aug 25, 2016 at 04:05 PM 0
Share

@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

avatar image aarontbarksdale btmedia14 · Aug 25, 2016 at 05:19 PM 0
Share

@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()

Show more comments
Show more comments
avatar image btmedia14 · Aug 26, 2016 at 04:37 PM 0
Share

@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

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

245 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

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


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