ZoomIn /ZoomOut : Null Reference exception


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class ZoomHandler : MonoBehaviour
{
    [SerializeField]  Camera camera;
    [SerializeField] float ZoomedOutFOV = 50;
    [SerializeField] float ZoomedInFOV = 20;
    [SerializeField] float ZoomedInSenesvity = 0.5f;
    [SerializeField] float ZoomedOutSenesvity = 2f;
    RigidbodyFirstPersonController FPScontroller;

   

    //private void OnDisable()
    //{
    //    ZoomOut();
    //}
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
           ZoomIN();
            

        }
        if (Input.GetMouseButtonUp(1))
        {
             ZoomOut(); 
            
        }
    }

    private void ZoomOut()
    {
        camera.fieldOfView = ZoomedOutFOV;
       
        FPScontroller.mouseLook.XSensitivity = ZoomedOutSenesvity;
        FPScontroller.mouseLook.YSensitivity = ZoomedOutSenesvity;
    }

    private void ZoomIN()
    {
        camera.fieldOfView = ZoomedInFOV;
        FPScontroller.mouseLook.XSensitivity = ZoomedInSenesvity;
        FPScontroller.mouseLook.YSensitivity = ZoomedInSenesvity;
    }
}

I am getting null reference exception in Zombie Runner in weapon Zoom script please help me.

Summary

This text will be hidden

Hi Ajit,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

Hope this helps :slight_smile:


See also;

1 Like

Hello there. I’m still in the process of learning but I think your problem is with this line:

[SerializeField] new Camera camera;

I don’t know what you’re trying to do here, but it looks like you’re trying to make a new camera? If that’s not the case and you’re trying to work with an existing camera, I believe you need to change it to this:

[SerializeReference] Camera camera;

And make sure your camera object is placed in this field in Unity. Sorry if this isn’t the real solution.

1 Like

I was using [SerializeField] Camera camera but that was not working and this is not error. I am getting error in x sensitivity.

But tq for ur answer :blush::blush::blush: I will try once with SerializeReference.

But I wanna tell u this that this code was working but after doing implement in (adding OnDisable function) I got this error. Also i tried to revert all charges but it didn’t worked

Help me now please @Nina ,I am getting error in FPScontroller.mouseLook.XSenitivity = ZoomedInSensevity
FPScontroller.mouseLook.XSenitivity = ZoomedOutSensevity

Sorry I just checked now and found that my
whole sensitivity code is giving me null reference exception because changed sr. Of x and u sensitivity and found this

Check with a Debug.Log which variable returns the error. Also check the spelling.

Which version of Unity do you use? Did you download the Standard Assets? If so, I would suggest to remove them from your project, download them from here, and import them again.

This topic was automatically closed after 17 hours. New replies are no longer allowed.

Privacy & Terms