NullReferenceException in the console driving me crazy

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

public enum PlayerState
{
walk,
attack,
interact,
stagger,
idle
}
public class PlayerMovement : MonoBehaviour
{
public PlayerState currentState;
public float speed;
[SerializeField] private Stat health;
bool attack;
private Animator animator;
public Rigidbody2D myRigidBody;
private Vector3 change;

private void Awake()
{
health.Initialize();
}
// Start is called before the first frame update
void Start()
{

currentState = PlayerState.walk;
myRigidBody = GetComponent();
animator = GetComponent();
animator.SetFloat(“Look X”, 0);
animator.SetFloat(“Look Y”, -1);
}

// Update is called once per frame

public void Knock(float knockTime, float damage)
{

//a place for damage stuff.
health.CurrentVal -= damage;
CombatTextMgr.Instance.CreateText(transform.position, damage.ToString(), Color.white);
StartCoroutine(KnockCo(knockTime));

}

void Update()
{

change = Vector3.zero;
change.x = Input.GetAxisRaw(“Horizontal”);
change.y = Input.GetAxisRaw(“Vertical”);
if(Input.GetButtonDown(“attack”) && currentState != PlayerState.attack && currentState != PlayerState.stagger)
{
StartCoroutine(AttackCo());
}
else if (currentState == PlayerState.walk || currentState == PlayerState.idle)

if (change != Vector3.zero)
{
MoveCharacter();
animator.SetFloat(“Look X”, change.x);
animator.SetFloat(“Look Y”, change.y);
animator.SetBool(“moving”, true);
}
else
{
animator.SetBool(“moving”, false);
}

}

private IEnumerator AttackCo()
{
animator.SetBool(“attack”, true);
currentState = PlayerState.attack;
yield return new WaitForSeconds(.4f);
animator.SetBool(“attack”, false);
currentState = PlayerState.walk;
}

void MoveCharacter()
{
change.Normalize();
myRigidBody.MovePosition(transform.position + change * speed * Time.deltaTime);
}
private IEnumerator KnockCo(float knockTime)
{
if (myRigidBody != null)
{

yield return new WaitForSeconds(knockTime);
myRigidBody.velocity = Vector2.zero;
currentState = PlayerState.idle;
myRigidBody.velocity = Vector2.zero;
}

}
}

The bolded line above is where it says the exception is located, but I can’t tell which part of it has the issue.

Use code tags for future reference

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

public enum PlayerState
{
walk,
attack,
interact,
stagger,
idle
}
public class PlayerMovement : MonoBehaviour
{
public PlayerState currentState;
public float speed;
[SerializeField] private Stat health;
bool attack;
private Animator animator;
public Rigidbody2D myRigidBody;
private Vector3 change;


private void Awake()
{
health.Initialize();
}
// Start is called before the first frame update
void Start()
{

currentState = PlayerState.walk;
myRigidBody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
animator.SetFloat("Look X", 0);
animator.SetFloat("Look Y", -1);
}

// Update is called once per frame



public void Knock(float knockTime, float damage)
{

//a place for damage stuff.
health.CurrentVal -= damage;
CombatTextMgr.Instance.CreateText(transform.position, damage.ToString(), Color.white);
StartCoroutine(KnockCo(knockTime));


}


void Update()
{


change = Vector3.zero;
change.x = Input.GetAxisRaw("Horizontal");
change.y = Input.GetAxisRaw("Vertical");
if(Input.GetButtonDown("attack") && currentState != PlayerState.attack && currentState != PlayerState.stagger)
{
StartCoroutine(AttackCo());
}
else if (currentState == PlayerState.walk || currentState == PlayerState.idle)

if (change != Vector3.zero)
{
MoveCharacter();
animator.SetFloat("Look X", change.x);
animator.SetFloat("Look Y", change.y);
animator.SetBool("moving", true);
}
else
{
animator.SetBool("moving", false);
}



}

private IEnumerator AttackCo()
{
animator.SetBool("attack", true);
currentState = PlayerState.attack;
yield return new WaitForSeconds(.4f);
animator.SetBool("attack", false);
currentState = PlayerState.walk;
}

void MoveCharacter()
{
change.Normalize();
myRigidBody.MovePosition(transform.position + change * speed * Time.deltaTime);
}
private IEnumerator KnockCo(float knockTime)
{
if (myRigidBody != null)
{

yield return new WaitForSeconds(knockTime);
myRigidBody.velocity = Vector2.zero;
currentState = PlayerState.idle;
myRigidBody.velocity = Vector2.zero;
}

}
}

It looks like this is referring to your script, CombatTextMgr.cs

I’m going to assume the CreateText() uses transform and a string variable?

I’m going to go out on a limb and say that its calling a Null becuase it cant find your script. I would also assume that you are declaring a static within the combat script? For example public static CombatTextMgr instance;
Or something like that… and then in start you declare instance = this;
? Am I on the right track?

I’d have to see your CombatTextMgr.cs to see what’s going on in that

I think it would be better to call this on Awake rather than Start. The Singleton pattern is very useful but can get a bit buggy and hairy if mismanaged or over-used.

1 Like