- Home /
I found the problem
Lerping the camera giving me an error
I am getting an error that says: Object reference not set to an instance of an object Here is my code: if (Input.GetButtonDown("Fire2")) { Camera.main.fieldOfView = Mathf.Lerp(60f, 30f , 10f); //Here } if (Input.GetButtonUp("Fire2")) { Camera.main.fieldOfView = Mathf.Lerp(30f, 60f, 10f); //And here }
Answer by IgnGrillo · Jan 18, 2021 at 09:25 PM
At first glance I see no problem with the code provided.
Remember that you can make the code a little easier to read if you use the provided tools in here
if (Input.GetButtonDown("Fire2"))
{
Camera.main.fieldOfView = Mathf.Lerp(60f, 30f, 10f);
}
if (Input.GetButtonUp("Fire2"))
{
Camera.main.fieldOfView = Mathf.Lerp(30f, 60f, 10f);
}
You are most likely getting a null reference exception because there is no Camera on your scene or because your camera's tag was modified by you or some other code
If you find yourself not finding where the camera tag is, take a look at this image:

p.s: You are not using the method Lerp as intended, the third parameter (Mathf.Lerp(60f, 30f, 10f)) should be a floating point value inbetween 0 and 1. If the provided T is outside this range, it will be clamped the same way as using mathf.clamp01
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Rigidbody Moveposition not working when Camera follows object? 0 Answers
Camera Relative Movement 2 Answers
Converting to 3rd Person from 1st Person - Island Demo 1 Answer
Game view camera snaps far back when my player is placed on to a terrain 0 Answers