NullReferenceException while following Unity3d College tutorial @JasonWeimann
I have followed the following tutorial as the setup for my state machine https://unity3d.college/2017/05/26/unity3d-design-patterns-state-basic-state-machine/
there is a null reference exception but I don't get why. Rigidbody, InputHandler and Character components all are attached to my character in the inspector. The RigidBody is attached to the Input handler and the Character class. Inputhandler does all the if(Input.GetButtonDown("blah blah")) stuff and returns some bools, it also checks for if grounded Inputhandler.grounded_ is a simple
h_vel = Rb.velocity.x;
grounded_ = (h_vel == 0f)? true : false;
and I have instantiated the rigidbody as Rb in InputHandler correctly
console is saying the issue is at Update() in character, and Tick() in Begin State. Please note at this point there are no forces being applied to the character so I'm considering the possibility that its not getting a value out of the rigid body, but I'm really not sure. Please help!
public class Character : MonoBehaviour
{
public State currentState;
public Rigidbody rb;
public Animator animator;
// Start is called before the first frame update
private void Start()
{
SetState(new BeginState(this));
}
// Update is called once per frame
private void Update()
{
currentState.Tick();
}
public void SetState(State state)
{
if (currentState != null)
{
currentState.OnStateExit();
}
currentState = state;
if (currentState != null)
{
currentState.OnStateEnter();
}
}
}
----------
public abstract class State
{
protected Character character;
protected float timescalar = 10f;
protected Animator animator;
public abstract void Tick();
public virtual void OnStateEnter(){}
public virtual void OnStateExit(){}
public State(Character character)
{
this.character = character;
}
}
public class BeginState : State
{
private InputHandler inputHandler;
public BeginState(Character character) : base(character) { }
public override void Tick()
{
if(inputHandler.grounded_)
{
character.SetState(new IdleState(character));
}
else
{
character.SetState(new AerialIdleState(character));
}
}
public override void OnStateEnter()
{
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612221245im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to understand the State Pattern - having trouble implementing 0 Answers
State Machine Tutorial Question 0 Answers
White line behind Object triggering collisions 0 Answers
Player animation FSM stopped working 0 Answers
Unity editor Slowness when starting 0 Answers