2
\$\begingroup\$

I can see in the Unity 3D documentation that its possible to get the altitude of the user from their iPhone, but I cannot figure out how to actually do it.

I try this code:

using UnityEngine;
using System.Collections;

public class Altitude : MonoBehaviour {

    void Start () {
        Input.location.Start();
    }

    void Update () {
        Debug.Log (LocationInfo.altitude);
    }
}

and I get this error:

Assets/Altitude.cs(11,41): error CS0120: An object reference is required to access non-static member `UnityEngine.LocationInfo.altitude'

\$\endgroup\$
3
\$\begingroup\$

LocationInfo is a struct so you need an instance of it to access its members. You can find a populated instance under (LocationService.lastData)[http://docs.unity3d.com/ScriptReference/LocationService-lastData.html] or Input.location.lastData references LocationService.lastData.

Meaning you can get the altitude like this:

void Start ()
{
    Input.location.Start();
}

void Update () {
    //lastData is an instance of LocationInfo
    Debug.Log (Input.location.lastData.altitude);
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you! I don't understand what a struct is, but you answered my question and it works! :) \$\endgroup\$
    – Twitch
    May 31 '15 at 22:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .