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 /
avatar image
0
Question by laurienash · Jun 01, 2016 at 08:18 PM · if-statementsnull reference exceptionaccessing scriptsspeedometer

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

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

44 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

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

Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer


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