• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 10, 2014 at 10:53 AM by Graham-Dunnett for the following reason:

Duplicate Question - has been asked over 700 times!

avatar image
0
Question by LlamaNervous · Jul 10, 2014 at 09:37 AM · cameranullreferenceexceptioncamera.mainmaincamera

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.

Comment
Comments Locked · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Jul 10, 2014 at 08:53 AM 0
Share

A nullref at runtime means that something is losing its reference. Something is beco$$anonymous$$g destroyed or assigning null after not finding something.

avatar image LlamaNervous · Jul 10, 2014 at 09:12 AM 0
Share

Are there any common causes for this?

avatar image LlamaNervous · Jul 10, 2014 at 09:17 AM 0
Share

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

avatar image Nerevar · Jul 10, 2014 at 09:54 AM 0
Share

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.

avatar image LlamaNervous · Jul 10, 2014 at 09:59 AM 0
Share

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!

avatar image Landern · Jul 10, 2014 at 10:04 AM 0
Share

Post the actual exception with line number, put the code/relevant code from the exception(when you double click on it, if it is in your code it "should" take you to the line the exception happened on) in this post, just saying you have a null reference exception doesn't help anyone actually pin point it other then telling you you have a un-instantiated references that is being references which is null.

avatar image LlamaNervous · Jul 10, 2014 at 10:05 AM 0
Share

In my build it doesn't let me click on the error, I have no idea where the error is co$$anonymous$$g from it doesn't show a line or anything it just stops my camera from looking up/down...

avatar image Landern · Jul 10, 2014 at 10:13 AM 0
Share

When I bring up the developer console in the build it comes up with NullReferenceException error? I think it's an error.

You state otherwise in your post.

avatar image Nerevar · Jul 10, 2014 at 10:15 AM 0
Share

in Start you could do something like this:

         try{
           mouseXSensitivity = PlayerPrefs.GetFloat("Sensitivity");
           mouseYSensitivity = PlayerPrefs.GetFloat("Sensitivity");

         }catch{
            Debug.Log("Error loading PlayerPrefs");
         }
         Screen.lockCursor = true;
         if(GetComponent<CharacterController>() == null)
               Debug.Log("Error Getting CharacterController reference");
         else
         characterController = GetComponent<CharacterController>();
 

I don't know if you can see the log messages in the build version, else use a GUI element to show the logs.

avatar image Landern · Jul 10, 2014 at 10:19 AM 0
Share

That try catch won't do anything, a float is not nullable(either declared as float? or Nullable in this case and will always have a default value of 0.0f if not explicitly set), the character controller however may be more of the sore spot. If this script is where the issues are.

Futher more, if GetFloat can't find the lookup, it returns default(float), so 0.0f.

1 Reply

  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Jul 10, 2014 at 10:53 AM

http://answers.unity3d.com/questions/topics/nullreferenceexception.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image majaus · Jan 15, 2016 at 11:50 PM 0
Share

Try to change the object name, works for me

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I'm getting a NullReferenceException on Camera.main 1 Answer

Null reference exception on Screenpointtoray (Multiplayer) 1 Answer

Occlusion Culling Issue (Walls/Objects Disappear) 0 Answers

C# camera script error 1 Answer

Yet another strange NullReferenceException 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges