Question by
PabloSky114 · Jul 24, 2016 at 03:14 PM ·
screentoworldpoint
NullReferenceException: Object reference not set to an instance of an object CarMovement.FixedUpdate ()
NullReferenceException: Object reference not set to an instance of an object CarMovement.FixedUpdate ()
This is the error I got on my game i'm creating. The movement was working at first, then it just stopped working suddenly?
using UnityEngine;
using System.Collections;
public class CarMovement : MonoBehaviour {
public float speedForce = 5;
public float maxSpeed = 50f;
public float rigidVelocity;
public bool oiled;
public int boost;
public bool boostOn;
public Vector3 mousePos;
// Use this for initialization
void Start () {
boost = 100;
oiled = false;
}
// Update is called once per frame
void Update () {
}
void FixedUpdate(){
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
rigidVelocity = GetComponent<Rigidbody2D> ().velocity.magnitude;
if (Input.GetKey (KeyCode.Mouse0)) {
if (rigidVelocity < maxSpeed && !oiled) {
GetComponent<Rigidbody2D> ().AddForce (transform.up * speedForce);
if (!Input.GetKey (KeyCode.Mouse1)) {
GetComponent<Rigidbody2D> ().drag = 0.2f;
speedForce = 5f;
}
}
if (Input.GetKey (KeyCode.Mouse1) && boost >= 1 && !oiled) {
GetComponent<Rigidbody2D> ().drag = 0.0f;
speedForce = 10f;
boost--;
boostOn = true;
}
} else {
if (!oiled) {
GetComponent<Rigidbody2D> ().drag = 1.5f;
}
}
if (!Input.GetKey (KeyCode.Mouse1)) {
boostOn = false;
}
if (boost == 0) {
boostOn = false;
GetComponent<Rigidbody2D> ().drag = 0.2f;
}
}
void OnTriggerEnter2D(Collider2D coll){
if (coll.gameObject.tag == "Oil") {
StartCoroutine (StartOiling());
}
}
IEnumerator StartOiling(){
oiled = true;
Debug.Log ("OILED");
yield return new WaitForSeconds (3);
Debug.Log ("UNOiled");
oiled = false;
Destroy (GameObject.Find ("Oil(Clone)"));
}
}
Code is messy, I know just getting used to it. The error is on line 29 where it has the code:
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
Comment