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