- Home /
Duplicate Question - has been asked over 700 times!
NullReferenceException in game build
I just built my first person horror game and upon a quick playthrough, when I reach the second level, the camera cannot move up or down but everything else can happen. When I went back to Unity to see what the problem was, it didn't happen, it only happens in the built version of the game. When I bring up the developer console in the build it comes up with NullReferenceException error? I think it's an error.
After searching the unity Answers for a while I tested that the camera in my controller is tagged as main camera and as far as I can tell, there are no other cameras in the scene.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour
{
public float forwardmovementSpeed = 7.0f;
public float sidemovementSpeed = 5.0f;
public float jumpSpeed = 0.1f;
public float mouseXSensitivity;
public float mouseYSensitivity;
float verticalRotation = 0;
public float upDownRange = 60.0f;
float verticalVelocity = 0;
CharacterController characterController;
public float Stamina = 10;
public bool Run;
void Awake ()
{
DontDestroyOnLoad(gameObject);
}
void Start ()
{
mouseXSensitivity = PlayerPrefs.GetFloat("Sensitivity");
mouseYSensitivity = PlayerPrefs.GetFloat("Sensitivity");
Screen.lockCursor = true;
characterController = GetComponent<CharacterController>();
Camera.main.fieldOfView = PlayerPrefs.GetInt("FOV");
}
void Update ()
{
//ROTATION
float rotLeftRight = Input.GetAxis("Mouse X") * mouseXSensitivity;
transform.Rotate (0, rotLeftRight, 0);
verticalRotation -= Input.GetAxis("Mouse Y") * mouseYSensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
//MOVEMENT
float forwardSpeed = Input.GetAxis("Vertical") * forwardmovementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * sidemovementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if(characterController.isGrounded && Input.GetButtonDown("Jump"))
{
verticalVelocity = jumpSpeed;
}
Vector3 speed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
characterController.Move(speed * Time.deltaTime);
if(Input.GetKeyDown(KeyCode.LeftShift))
{
if(Stamina > 0)
{
Run = true;
forwardmovementSpeed = 7f;
sidemovementSpeed = 5f;
}
}
if(Stamina <= 0)
{
forwardmovementSpeed = 3f;
sidemovementSpeed = 3f;
Run = false;
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
forwardmovementSpeed = 3f;
sidemovementSpeed = 3f;
Run = false;
}
if(Run == true)
{
Stamina -= Time.deltaTime;
}
else
{
if(Stamina < 10)
{
Stamina += Time.deltaTime;
}
}
if(Input.GetKeyDown(KeyCode.F1))
{
Application.LoadLevel(3);
}
}
}
Any help would be appreciated.
A nullref at runtime means that something is losing its reference. Something is beco$$anonymous$$g destroyed or assigning null after not finding something.
I am using PlayerPrefs to get the fpc's sensitivity at the start of each scene.. is there a problem with that?
It doesn't happen on any other level
Hello
It seems to come from the camera and the script piloting it.
You should check if a component or an object exists before trying to access/manipulate it (with a Debug.Log or try/catch ..), it will be safer and easier for you to debug after that.
I have spent all day trying to figure this out so I can build the game. I can't find anything that could cause this anywhere! I edited in my script, please have a look at it and see if you can find anything that would cause this!
Follow this Question
Related Questions
I'm getting a NullReferenceException on Camera.main 1 Answer
Need help for camera view like Air Strike 3d game ? 1 Answer
Is there any way to keep game object withing the limit of Camera FOV 3 Answers
How can using a static member (Camera) cause a NullReferenceException? 1 Answer
Switching camera's error 2 Answers