Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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!

Show more comments

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

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

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


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