Howdy, Stranger!

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

Mega 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 Answer

  • MouledouxMouledoux Member
    Accepted Answer

    Is the camera in your scene tagged as 'MainCamera'?

Answers

  • Yeah I tagged it as main camera and now it works fine. Thank u very much for helping

Sign In or Register to comment.