[HELP] Rotation in top down

So i have a problem with player rotating to mouse and the problem is that it it doesnt work and spews out:
NullReferenceException: Object reference not set to an instance of an object
RotateToCursor.rotateToCamera () (at Assets/Scripts/RotateToCursor.cs:22)
RotateToCursor.Update () (at Assets/Scripts/RotateToCursor.cs:14)

Script:

    Vector3 mousePos;
    Rigidbody2D bod;

    // Use this for initialization
    void Start () {
        bod = this.GetComponent<Rigidbody2D> ();
    }
   
    // Update is called once per frame
    void Update () {
        rotateToCamera ();
    }




    void rotateToCamera()
    {
        mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        Quaternion rot = Quaternion.LookRotation (transform.position - mousePos, Vector3.forward);

        transform.rotation = rot;
        transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);
        bod.angularVelocity = 0;
}

Thanks

You have removed part of the script, so what actually is the line 22? I would guess it’s referring to the “bod” variable being null.

line 22 is making the objects rotation = to rotation in line 20

No, I’m asking what is on the line 22 in the actual file. Your code sample is missing pieces (the class definition for example) and the error is referring a problem on line 22 (not the line 22 in the sample, but in the file).

Anyway, make sure your GameObject has Rigidbody2D, otherwise “bod” would be null.