Home > Enterprise >  How do I get this error? I want my character to move with wasd and it prints 100 errors
How do I get this error? I want my character to move with wasd and it prints 100 errors

Time:12-05

NullReferenceException: Object reference not set to an instance of an object

StarterAssets.ThirdPersonController.Move () (at Assets/Scripts/ThirdPersonController.cs:258)

StarterAssets.ThirdPersonController.Update () (at Assets/Scripts/ThirdPersonController.cs:161)

from 155 to 161 line:

private void Update()
        {
            _hasAnimator = TryGetComponent(out _animator);

            JumpAndGravity();
            GroundedCheck();
            Move();

from 257 to 265

{
                _targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg  
                                  _mainCamera.transform.eulerAngles.y;
                float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _targetRotation, ref _rotationVelocity,
                    RotationSmoothTime);

                // rotate to face input direction relative to camera position
                transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f);
            }

What causes the error?

CodePudding user response:

Errors like these just points that you are either accessing a private/non-existent variable/method. Check whether if you put declare public on your variables.

CodePudding user response:

In Unity, the most times i see this Exception, it is because I did not set a public variable or a [SerializeField] in the Inspector. If you then try to use it in your script, this Error occures.

  • Related