- Home /
Camera NullReferenceException
I have the following script attached to my camera.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class ScaleWidthCamera : MonoBehaviour
{
public float targetWidth = 1920;
public float FOV = 42f;
void OnGUI()
{
float height = (targetWidth / Screen.width) * Screen.height;
camera.orthographicSize = (height / FOV) / 2f;
}
}
Sometimes when I'm in the editor I get a NullreferenceException error on the camera. Along with !CompareApproximately (det, 1.0F, .005F)
This makes absolutely no sense since camera clearly exists and I only get this error once. When I run the game this script works just fine (It is used by a slider that adjusts the FOV value).
Before you say anything about using OnGUI(), I did that because if the FOV is changed during Update() or FixedUpdate(), it causes a stuttering movement as the FOV is changed.
Can you tell us with the you are setting the orthographicSize to when you get the error? What you've set FOV to?
I have another script that references this one. The reference is set in the inspector. The slider works perfectly in game. This error only shows up in the editor. Usually when loading that scene or at the end of a build.
Answer by lolzrofl · Nov 04, 2014 at 06:38 AM
Ok I just fixed this myself. Apparently the editor was trying to change the FOV value before all the references were setup in my prefabs. Making FOV static got rid of the errors.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pinch zoom 0 Answers
How can I display text so that it can be see on the Vive Headset? 0 Answers