Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Script problem

I am using this script to rotate the gun using mouse pointer,

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Weapon : MonoBehaviour


  public float offset;

  private void Update()


    Vector3 difference = Camera.main.ScreenToViewportPoint(Input.mousePosition) - transform.position;

    float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

    transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);



but in unity it showing an error,

NullReferenceException: Object reference not set to an instance of an object

Weapon.Update () (at Assets/Scripts/Weapon.cs:11)

Best Answers


  • If you figured out the answer on your own then comment it so others can learn from it too

  • ShounakGhoshShounakGhosh Member
    edited July 26

    Well I thought I figured out the problem and commented, but it didn't work. I am an idiot and don't know how to delete a comment so I deleted the content.

Sign In or Register to comment.