NullReferenceException for an argument of a method

Hello,

I am having some trouble with one of my scripts. I am receiving a NullReferenceException on a methods arguement. The error message reads as follows:

Game.PlayerGroundedState.SetSlopeSpeedMod (System.Single angle) (at Assets/Scripts/Player/StateMachines/Movement/States/Grounded/PlayerGroundedState.cs:61)```

Here is the code I am having trouble with:

**PlayerGroundedState.cs**

```csharp
public class PlayerGroundedState : PlayerMovementState
    {
        private SlopeData SlopeData;
        private PlayerGroundedData GroundedData;

        public PlayerGroundedState(PlayerMovementStateMachine PlayerMovementStateMachine) : base(PlayerMovementStateMachine)
        {
            SlopeData = StateMachine.PlayerHandler.CapsuleUtility.SlopeData;
        }

        #region IState Methods
        public override void PhysicsUpdate()
        {
            base.PhysicsUpdate();

            CapsuleFloat();
        }

        #endregion

        #region Main Methods
        private void CapsuleFloat()
        {
            Vector3 CapsuleCenterWS = StateMachine.PlayerHandler.CapsuleUtility.CapsuleColliderData.Collider.bounds.center;
            Ray RayFromCapsuleCentre = new Ray(CapsuleCenterWS, Vector3.down);

            if (Physics.Raycast(RayFromCapsuleCentre, out RaycastHit RayOut, SlopeData.RayDistance, StateMachine.PlayerHandler.LayerData.GroundLayer, QueryTriggerInteraction.Ignore))
            {
                float AngleFromGround = Vector3.Angle(RayOut.normal, -RayFromCapsuleCentre.direction);
                float DistanceFromGround = StateMachine.PlayerHandler.CapsuleUtility.CapsuleColliderData.ColliderCenterLS.y * StateMachine.PlayerHandler.transform.localScale.y - RayOut.distance;

                float SlopeSpeedModifier = SetSlopeSpeedMod(AngleFromGround);

                if (DistanceFromGround == 0f)
                {
                    return;
                }

                if (SlopeSpeedModifier == 0f)
                {
                    return;
                }

                float TargetDistanceFromGround = DistanceFromGround * SlopeData.StepReachForce - GetVerticalVelocity().y;
                Vector3 ForceToApply = new Vector3(0f, TargetDistanceFromGround, 0f);

                StateMachine.PlayerHandler.Rigidbody.AddForce(ForceToApply, ForceMode.VelocityChange);
            }
        }

        private float SetSlopeSpeedMod(float angle)
        {
            float SlopeSpeedModifier = GroundedData.SlopeSpeedAngles.Evaluate(angle);
            StateMachine.ReusableData.SlopeSpeedMod = SlopeSpeedModifier;
            return SlopeSpeedModifier;
        }
        #endregion

SlopeData.cs

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;

namespace Game
{
    [Serializable]
    public class SlopeData
    {
        [field: SerializeField] [field: Range(0f, 1f)] public float StepHeightPercentage { get; private set; } = 0.25f;
        [field: SerializeField][field: Range(0f, 5f)] public float RayDistance { get; private set; } = 2f;
        [field: SerializeField][field: Range(0f, 50f)] public float StepReachForce { get; private set; } = 25f;
    }
}

PlayerGroundedData.cs

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;

namespace Game
{
    [Serializable]
    public class PlayerGroundedData
    {
        [field:SerializeField] public PlayerRotationData BaseRotationData { get; private set; }
        [field:SerializeField] public PlayerWalkData WalkData { get; private set; }
        [field:SerializeField] public PlayerRunData RunData { get; private set; }

        [field:SerializeField] [field: Range(0f, 25f)] public float BaseSpeed { get; private set; } = 5f;
        [field:SerializeField] public AnimationCurve SlopeSpeedAngles { get; private set; }
    }
}

Any help will be greatly appreciated.

The answer for null reference exception is ALWAYs the same. It doesn’t even matter what you’re doing.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

I also see quite a few big long hairy lines of code up there.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

Thank you so much for your help. I forgot to initialise the GroudedData variable in the PlayerHandler constructor. I would like to say your process to resolving these kinds of errors is really helpful.

Once again,
Thank you

1 Like

Thanks! It also has the side benefit that over decades of programming it has never failed, not even once.