- Home /
Moving object depending on player's GPS movement: null reference exception with accessing speed variable = if (speedGPS.speedInKmPerHour >=5f)
Hello,
I'm trying to move an object depending on the speed that the player is travelling at in real life (using GPS): I have the two scripts separately, but I'm having trouble accessing the speed variable in the speedometer script using this line: if (speedGPS.speedInKmPerHour >=5f)
As it's throwing the Null Reference Exception: object reference not set to an instance of an object.
This is the movement script (which moves two terrains to give the appearance of the object moving):
public class MoveTerrain : MonoBehaviour {
public float moveForwards = 20f;
public GameObject terrain;
public Vector3 targetPos;
public GameObject otherTerrain;
private Speedometer speedGPS;
static public float speed = 10.0f;
void Update () {
if (speedGPS.speedInKmPerHour >=5f)
// if (Input.GetKey("w"))
terrain.transform.Translate(Vector3.forward * Time.deltaTime * moveForwards * speed);
}
void LateUpdate()
{
if (targetPos.z - terrain.transform.position.z < 10f)
terrain.transform.position = otherTerrain.transform.position + new Vector3(0.0f, 0.0f, -250);
}
}
And this is the speedometer script that accesses the speed the player is moving at:
public float speedInKmPerHour = 0f;
public float timeframe = 0f;
public int recordSize = 2;
private List<LocationInfo> record = new List<LocationInfo> ();
private LocationServiceController service;
// -------------------------------------------------------------------
/* Request location services to start */
void Start(){
service = gameObject.AddComponent<LocationServiceController> ();
}
/* Wait until services are ready */
void Update(){
if (!service.ready)return;
AddValidSample ();
if (record.Count >= 2) {
UpdateSpeed (forelast, last);
}
}
void AddValidSample(){
LocationInfo sample = current;
// discard duplicates
if (record.Count > 0 && sample.timestamp == last.timestamp) {
return;
}
record.Add (sample);
while (record.Count > recordSize) record.RemoveAt (0);
}
void UpdateSpeed(LocationInfo a, LocationInfo b){
float D = Speedometer.distInKm (a, b);
timeframe = (float)(b.timestamp - a.timestamp);
float speedInKmPerSecond = D / timeframe;
speedInKmPerHour = speedInKmPerSecond * 3600;
}
LocationInfo last { get { return record [record.Count-1]; } }
LocationInfo forelast { get { return record [record.Count-2]; } }
LocationInfo current { get { return Input.location.lastData; } }
public static float distInKm(LocationInfo A, LocationInfo B){
float lat1 = A.latitude;
float lon1 = A.longitude;
float lat2 = B.latitude;
float lon2 = B.longitude;
float R = 6371e3f; // ~ earth radius in metres
float φ1 = lat1*Mathf.Deg2Rad;
float φ2 = lat2*Mathf.Deg2Rad;
float Δφ = (lat2-lat1)*Mathf.Deg2Rad;
float Δλ = (lon2-lon1)*Mathf.Deg2Rad;
float a = Mathf.Sin(Δφ/2) * Mathf.Sin(Δφ/2)
+ Mathf.Cos(φ1) * Mathf.Cos(φ2) * Mathf.Sin(Δλ/2) * Mathf.Sin(Δλ/2);
float c = 2f * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1f-a));
float d = R * c;
return (float)d * 0.001f;
}
}
Does anyone know what I'm doing wrong?
All the best,
Laurien
Answer by ralphlizard · Jun 02, 2016 at 08:41 AM
You must create an instance of your Speedometer to access its variables.
private Speedometer speedGPS;
creates speedGPS which is null. Instead, you need to use the constructor
private Speedometer speedGPS = new Speedometer();
Your answer
![](https://koobas.hobune.stream/wayback/20220612082105im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GetComponent null reference C# 1 Answer
Different Explosion for Different Collisions 3 Answers
Problem with Counting Collisions 2 Answers
contacts.normal question 1 Answer