Hello, I am making a 2D pixel Art game, and I am using state machine behaviors to make an enemy chase the player. However, whenever the enemy gets within chase range, the first line in both OnStateEnter AND OnStateUpdate hits the console with a NullReferenceException. Does anyone else get this error? Is it a bug? How do I fix it?
(Errors on line 29 and 45 of Chase Script)
Chase Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicZombieChaseBehaviour : StateMachineBehaviour
{
private Transform player;
public float distanceFromPlayer;
public float speed;
public Vector2 movement;
Transform pos;
public float attackCooldown;
public float startAttackCooldown;
public float damageToPlayer;
public Transform[] attackPos;
public float attackRange;
public int attackSide;
public LayerMask enemyLayers;
public PlayerStats playerStats;
public BasicZombie basicZombie;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
basicZombie = animator.transform.parent.GetComponent<BasicZombie>();
attackPos = basicZombie.attackPositions;
startAttackCooldown = 0.35f;
pos = animator.transform;
speed = 2.25f;
player = GameObject.FindGameObjectWithTag("Player").transform;
playerStats = player.GetComponent<PlayerStats>();
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
movement.x = pos.position.x - player.position.x;
movement.y = pos.position.y - player.position.y;
animator.SetFloat("Horizontal", -movement.x);
animator.SetFloat("Vertical", -movement.y);
distanceFromPlayer = Vector2.Distance(player.position, animator.transform.position);
animator.SetFloat("playerDist", distanceFromPlayer);
animator.transform.position = Vector2.MoveTowards(animator.transform.position, player.position, speed * Time.deltaTime);
if (distanceFromPlayer <= 0.65)
{
if(attackCooldown <= 0)
{
hitTarget(animator);
} else
{
attackCooldown -= Time.deltaTime;
}
}
if (distanceFromPlayer > 7)
{
animator.SetBool("isChasing", false);
}
}
public void hitTarget(Animator animator)
{
attackCooldown = startAttackCooldown;
sideFacing(animator);
attackSide = animator.GetInteger("SideFacing");
Collider2D[] enemiesToHit = Physics2D.OverlapCircleAll(attackPos[attackSide].position, attackRange, enemyLayers);
for (int i = 0; i < enemiesToHit.Length; i++)
{
damageToPlayer = playerStats.damagePerHit;
enemiesToHit.GetComponent<PlayerStats>().takeDamage(damageToPlayer);
}
}
public void sideFacing(Animator animator)
{
if (animator.GetFloat("Vertical") == -1 || animator.GetFloat("Vertical") == -1 && animator.GetFloat("Horizontal") == 1 || animator.GetFloat("Vertical") == -1 && animator.GetFloat("Horizontal") == -1)
{
animator.SetInteger("SideFacing", 2);
}
else if (animator.GetFloat("Vertical") == 1 || animator.GetFloat("Vertical") == 1 && animator.GetFloat("Horizontal") == 1 || animator.GetFloat("Vertical") == 1 && animator.GetFloat("Horizontal") == -1)
{
animator.SetInteger("SideFacing", 0);
}
else if (animator.GetFloat("Horizontal") == 1)
{
animator.SetInteger("SideFacing", 1);
}
else if (animator.GetFloat("Horizontal") == -1)
{
animator.SetInteger("SideFacing", 3);
}
}
}
Aaand my Basic Zombie Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicZombie : MonoBehaviour
{
public float health;
public Transform[] attackPositions;
private void Update()
{
if(health <= 0)
{
die();
}
}
public void die()
{
Debug.Log("Enemy Killed");
Destroy(gameObject);
}
public void takeDamage(float damage)
{
health -= damage;
}
}
Player Stats Script:
```csharp
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStats : MonoBehaviour
{
public Animator animator;
public bool hasKnife;
public bool hasFists;
public float health;
public float maxHealth;
public float hunger;
public float maxHunger;
float hungerDecreaseRate = 1.5f;
public float thirst;
public float maxThirst;
float thirstDecreaseRate = 2f;
public bool isDead;
public float hasNoHungerHealthDecrease;
public float hasNoThirstHealthDecrease;
public bool itemEquipped;
public float damagePerHit;
public void Start()
{
hasFists = true;
isDead = false;
health = maxHealth;
hunger = maxHunger;
thirst = maxThirst;
itemEquipped = false;
}
public void FixedUpdate()
{
if (hasKnife)
{
damagePerHit = 15;
} else if (hasFists) {
damagePerHit = 5;
}
if (!isDead && hunger > 0)
{
hunger = hunger - hungerDecreaseRate * Time.fixedDeltaTime;
}
if(!isDead && thirst > 0)
{
thirst = thirst - thirstDecreaseRate * Time.fixedDeltaTime;
}
if (thirst <= 0 && !isDead)
{
health = health - hasNoThirstHealthDecrease * Time.fixedDeltaTime;
}
if (hunger <= 0 && !isDead)
{
health = health - hasNoHungerHealthDecrease * Time.fixedDeltaTime;
}
if (health <= 0)
{
isDead = true;
}
}
public void equipItem(string item)
{
//Debug.Log(item);
if(item == "knife")
{
hasKnife = !hasKnife;
if(!hasKnife)
{
hasFists = true;
} else {
hasFists = false;
}
animator.SetBool("HasKnife", hasKnife);
}
}
public void takeDamage(float damage)
{
health -= damage;
}
}*
```
Help would be greatly appreciated! Thank you!