on mouse down returns null exception error

i am trying to move a football up when I click on it.its an 2D game. but I get an error saying “Null reference Exception:object reference not set to an instance of an object” here is the code I typed.

I am new to Unity as well as to scripting.

using UnityEngine;
using System.Collections;
public class playercontrol : MonoBehaviour
{
public int force;
private Rigidbody rb;
void start()
{
rb = GetComponent();

}
// Update is called once per frame
void Update ()
{
var dist = (transform.position - Camera.main.transform.position).z;
float lb= Camera.main.ViewportToWorldPoint (new Vector3(0, 0, dist)).x ;
float rb = Camera.main.ViewportToWorldPoint (new Vector3(1, 0, dist)).x ;
float bt= Camera.main.ViewportToWorldPoint (new Vector3(0, 0, dist)).y ;
float tp = Camera.main.ViewportToWorldPoint (new Vector3(0, 1, dist)).y;
var x = Mathf.Clamp(transform.position.x,lb,rb);
var y = Mathf.Clamp(transform.position.y,bt,tp);
var z = transform.position.z;
transform.position = new Vector3 (x, y, z);
}
void OnMouseDown()
{
rb.AddForce (transform.up*10f);
}
}

If I use debug.log in OnMouseDown function the script is working fine but AddForce is returning an error “object reference not set to an instance of the object”.
please help…thanks

You said its 2D so I guess you have a Rigidbody2D attached, but in your script your trying to call a normal Rigidbody

Thanks for the reply. Initially I tried with a rigid body 2D component. It gave the same error. So I removed the 2d component in the inspector and. Attached a normal rigid body component.
My object it an png picture file which I dragged into the scene view. Do I have to change any setting before using it as rigid body?

@Vionix , your void start()'s start() is in lowercase, assuming you copy and paste the whole code without editing, your Start function need to start with a capital S. Maybe that is the problem. Try void Start()

1 Like

yes you were right. thank you. I got it working:)