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:
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;
}
}
}
}
$$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
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?
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
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.)
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
Is the component enabled?
Have you tried re-installing Unity?
Can you post a screenshot of Unity while inspecting your player object?
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.