Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by nickostan · Feb 21, 2016 at 03:47 AM · bugnullreferenceexception

Restarting unity breaks game (BUG?)

Hey,

This has been happening for a couple days now. I feel inclined to submit it as a bug, but want to ensure it is one first before I do so.

I will be adding stuff to my game, everything works and tests well, and then all of a sudden I get NullReferenceException errors:

alt text

Even though I haven't even touched those scripts. Occasionally, adding and modifying animations in the animation pane causes this to happen.

I just restarted unity after saving my code, scene, and everything while it was all WORKING. I re-loaded Unity and it froze upon trying to open VisStudio 3x. I had to restart my PC.

When I reloaded Unity, it was able to open VisStudio. I played my game and got the above two errors, even though they were not there prior to me closing unity.

I swear this happens every few hours. Unity picks a line in my code and throws a NullReferenceException. I have attached my code (a simplified form) so you can see that I'm indeed referencing an object.

Any ideas? Really frustrating.

     private CharacterController _charCont;    
     private Camera viewCamera;
 void Start()
     {
         _charCont = GetComponent<CharacterController>();
         viewCamera = Camera.main;
     }
 
 void Update()
 {
 movelisten();
 cameraFollow();
 }
 
     void moveListen()
     {
 
         //move code up here removed
         _charCont.Move(movement);  // *****ERROR THROWN HERE
 
     }
 
     void cameraFollow()
     {
         float charPosX = transform.position.x;
         float charPosZ = transform.position.z;
         float cameraOffset = 18.0f;
 
         viewCamera.transform.position = new Vector3(charPosX, cameraOffset, charPosZ);  // ****ERROR THROWN HERE AS WELL
 
     }


HERE IS THE FULL CODE, IN CASE IT HELPS.

 using UnityEngine;
 using System.Collections;
 
 public class TD_CharacterController : MonoBehaviour
 {
     public string charName;
     public int charLevel = 1;
     public int charExp = 0;
 
     public float moveSpeed = 10.75f;
     public float sprintSpeed = 12.5f;
     public float crouchSpeed = 5.0f;
     public float ms;
     public float shotAdd = 0.0f;
 
     public bool isRunning = false;
     public bool isSprinting = false;
     public bool isCrouching = false;
     public bool isShooting = false;
     public bool isIdle = true;
     public bool isKeypad = false;
 
     public float gravity = -20f;
     public GameObject bullet_std;
     public GameObject _charFace;
     public GameObject muzzleLight;
     public Vector3 aimDirection;
     public Vector3 animValues;
 
     public CharacterController _charCont;
     public Camera viewCamera;
 
     public float deltaX;
     public float deltaZ;
 
     public GameObject flashLight;
     private bool toggleLight;
 
     AudioManager audioManager;
     GunController gunController;
     SphereCollider aggroSphere;
     Animator anim;
     CanvasController __cc;
 
     // Use this for initialization
     void Start()
     {
         audioManager = GameObject.Find("AudioManager").GetComponent<AudioManager>();
         anim = GetComponent<Animator>();
         gunController = GetComponent<GunController>();
         __cc = GameObject.Find("UI Manager").GetComponent<CanvasController>();
         aggroSphere = GetComponent<SphereCollider>();
         _charCont = GetComponent<CharacterController>();
         viewCamera = Camera.main;
         muzzleLight.SetActive(false);
         flashLight.SetActive(false);
         animValues = new Vector3(0f, 0f, 0f);
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         //listen for movement
         moveListen();
 
         //listen for look rotation
         lookListen();
 
         //attack listen
         shootListen();
 
         //gun manager listener
         gunController.weaponSwitchListen();
 
         //listen for reloads
         reloadListen();
 
         //flashlight
         flashLightListen();
 
         //keypad input
         keyPadListen();
 
     }
 
     void LateUpdate()
     {
         //camera follow
         cameraFollow();
     }
 
     void moveListen()
     {
         ms = moveSpeed;
 
         deltaZ = Input.GetAxis("Vertical") * ms;
         deltaX = Input.GetAxis("Horizontal") * ms;
 
         if (Input.GetKey(KeyCode.LeftShift))
         {
             ms = sprintSpeed;
             isSprinting = true;
             isRunning = false;
             isCrouching = false;
             isIdle = false;
         }
         else if (Input.GetKey(KeyCode.LeftControl))
         {
             ms = crouchSpeed;
             isSprinting = false;
             isRunning = false;
             isCrouching = true;
             isIdle = false;
         }
         else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)
              && !Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.LeftControl))
         {
             ms = moveSpeed;
             isSprinting = false;
             isRunning = true;
             isCrouching = false;
             isIdle = false;
         }
         else
         {
             isSprinting = false;
             isRunning = false;
             isCrouching = false;
             isIdle = true;
         }
 
         Vector3 movement = new Vector3(deltaX, gravity, deltaZ);
         movement = Vector3.ClampMagnitude(movement, ms);
         movement = movement * Time.deltaTime;
 
         animValues.x = deltaX;
         animValues.z = deltaZ;
 
         animValues = Vector3.Scale(animValues, transform.forward);
 
         _charCont.Move(movement);
 
     }
 
     void lookListen()
     {
         Vector3 mousePos = viewCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, viewCamera.transform.position.y));
 
         transform.LookAt(mousePos + Vector3.up * transform.position.y);
 
         aimDirection = (mousePos - transform.position);
         aimDirection = aimDirection.normalized;
         aimDirection.y = 0f;
 
     }
 
     void cameraFollow()
     {
         float charPosX = transform.position.x;
         float charPosZ = transform.position.z;
         float cameraOffset = 18.0f;
 
         viewCamera.transform.position = new Vector3(charPosX, cameraOffset, charPosZ);
 
     }
 
     void shootListen()
     {
         if (Input.GetKey(KeyCode.Mouse0) && !isKeypad)
         {
             isShooting = true;
             gunController.shoot();
         }
         else
         {
             isShooting = false;
         }
     }
 
     void reloadListen()
     {
         //print("FUNCTION WAS CALLED");
         if (Input.GetKeyDown(KeyCode.R))
         {
             //print("R KEY WAS PRESSED");
             gunController.clipReload(gunController.equippedWeapon);
         }
     }
 
     void flashLightListen()
     {
         if (Input.GetKeyDown(KeyCode.F))
         {
             toggleLight = !toggleLight;
             print(toggleLight);
             flashLight.SetActive(toggleLight);
 
         }
     }
 
     void keyPadListen()
     {
 
         if (!isKeypad)
         {
             __cc.codeDisplay(false);
         }
 
         if (Input.GetKeyDown(KeyCode.E))
             {
             if (isKeypad && isShooting == false)
                 {
                     __cc.codeDisplay(true);
                     isShooting = true;
                 }
             else if(isKeypad && isShooting == true)
             {
                 __cc.codeDisplay(false);
                 isShooting = false;
             }
             }
     }
 }
 
 

error.png (9.3 kB)
Comment
Add comment · Show 5
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 nickostan · Feb 21, 2016 at 03:53 AM 0
Share

$$anonymous$$y game is completely broken now, and I have no clue how to fix it... everything is properly referenced, as it was before, but for some reason the TD_CharacterController script refuses to reference the CharacterController component and main Camera............ sigh

avatar image nickostan · Feb 21, 2016 at 04:01 AM 0
Share

I changed my variables to public, and referenced them in the inspector. It fixed the errors on those, but then chose another line to throw a NullReferenceException on. As I fix one... it just chooses another and another and another... it's like GetComponent isn't working, or only works periodically?

avatar image nickostan · Feb 21, 2016 at 04:13 AM 0
Share

Currently, as I start and stop gameplay, it is changing errors. Choosing a different line to throw NullReferenceException on each time. EVEN THOUGH they were working the last time I had unity open, and are properly referenced

avatar image nickostan · Feb 21, 2016 at 04:14 AM 0
Share

alt text

new-error2.png (4.5 kB)
avatar image nickostan · Feb 21, 2016 at 04:14 AM 0
Share

alt text

new-error.png (4.4 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mikey555 · Feb 21, 2016 at 10:19 AM

The error you're getting means Unity is trying to access a null object. In your case, the error is thrown whenever _charCont is accessed.

The line is the problem:

 _charCont = GetComponent<CharacterController>();

Are you sure that the component CharacterController has been attached to the gameObject that this script is attached to? (TD_CharacterController and CharacterController components should both be attached to your gameObject.)

Comment
Add comment · Show 4 · 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 nickostan · Feb 21, 2016 at 02:33 PM 0
Share

Indeed, all of the getcomponent references without reference to a game object... theyre all on my player object. Anything not on my player object... such as the canvascontroller, i prefix with a GameObject.Find.

These lines of code like I said worked perfectly, but then seem to randomly choose to throw errors from time to time

avatar image mikey555 nickostan · Feb 22, 2016 at 01:12 AM 0
Share
  • Is the component enabled?

  • Have you tried re-installing Unity?

  • Can you post a screenshot of Unity while inspecting your player object?

avatar image nickostan mikey555 · Feb 22, 2016 at 02:44 AM 0
Share

alt text

Yes everything is enabled. No I have not tried re-installing unity. If this persists, and nobody can find a fix, I will consider doing that.

error-flow.png (130.8 kB)
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

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

Related Questions

UnityEngine.UI.Image keeps removing itself from a Prefab. 1 Answer

UI null reference errors in script, but not in inspector. 1 Answer

NullReferenceException involving loading windows?! 2 Answers

My Unity Makes USB Attached Sound 0 Answers

Launch app and get crash on Win7 SP1(Unity 2017.4) 0 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