Object reference not set to an instance of an object [HELP!]
I'm getting this error but I have no clue whats wrong with my code. I am not getting any compile errors, but errors when I start the game. The error is:
NullReferenceException: Object reference not set to an instance of an object PlayerCamera.CameraMovement () (at Assets/Scripts/PlayerCamera.cs:185) PlayerCamera.LateUpdate () (at Assets/Scripts/PlayerCamera.cs:141)
Here is the code that gives me the error:
public void CameraMovement()
{
if (playerController.aim)
{
Camera.main.fieldOfView = Mathf.Lerp (Camera.main.fieldOfView, zoomFOV, _deltaTime * lerpSpeed);
_camDir = (aimDirection.x * target.forward) + (aimDirection.z * target.right);
_targetHeight = normalAimHeight;
_targetDistance = normalAimDistance;
}
else
{
Camera.main.fieldOfView = Mathf.Lerp (Camera.main.fieldOfView, normalFOV, _deltaTime * lerpSpeed); //Error on this line
_camDir = (normalDirection.x * target.forward) + (normalDirection.z * target.right);
_targetHeight = normalHeight;
_targetDistance = normalDistance;
}
It seems like the actual problem might lie in the initialization of the variables you are using. Can you post the sections of the script where you declare and assign them?
Your answer
Follow this Question
Related Questions
Keep Getting NullReferenceException: Object reference not set to an instance of an object 0 Answers
Array of dictionaries: "Object reference not set to an instance of an object" 0 Answers
Object reference not set to an instance of an Object C# 2 Answers
How to fix a compiler error? 2 Answers
An instance of type 'UnityEngine.Behaviour' is required to access non static member 'enabled'. 0 Answers