Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

on mouse down returns null exception error

Discussion in '2D' started by Vionix, Jun 14, 2015.

  1. Vionix

    Vionix

    Joined:
    Jun 11, 2015
    Posts:
    12
    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:eek:bject 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<Rigidbody>();

    }
    // 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);
    }
    }
     
  2. Vionix

    Vionix

    Joined:
    Jun 11, 2015
    Posts:
    12
    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
     
  3. Gardes

    Gardes

    Joined:
    Apr 7, 2015
    Posts:
    46
    You said its 2D so I guess you have a Rigidbody2D attached, but in your script your trying to call a normal Rigidbody
     
  4. Vionix

    Vionix

    Joined:
    Jun 11, 2015
    Posts:
    12
    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?
     
  5. Kuan

    Kuan

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    87
    @Vionix, your
    Code (CSharp):
    1. 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
    Code (CSharp):
    1. void Start()
     
    Gardes likes this.
  6. Vionix

    Vionix

    Joined:
    Jun 11, 2015
    Posts:
    12
    yes you were right. thank you. I got it working:)