Hi, i am getting the following error in my script on line 35 of my EnemyAttack script.
NullReferenceException: Object reference not set to an instance of an object
EnemyAttack.Update () (at Assets/Scripts/EnemyAttack.cs:35)
**EnemyAttack script:**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour
{
public float attackIntervals; //time between attacks
public int attackDamage = 15; //enemy damage given
Animator anim; //references animator
GameObject player; //references the player
PlayerHealth playerHealth; //references the player health
bool playerRange; //range from enemy to player
float timer; //timer for attacking
// Start is called before the first frame update
void Awake()
{
player = GameObject.FindWithTag("Player");
playerHealth = player.GetComponent();
anim = GetComponent();
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (timer >= attackIntervals && playerRange)
{
Attack();
}
if (playerHealth.currentHealth <= 0)
{
anim.SetTrigger("isDead");
}
}
//attack the player
void Attack()
{
timer = 0f;
if (playerHealth.currentHealth > 0)
{
playerHealth.TakingDamage(attackDamage);
}
}
//trigger collider function on entry
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
playerRange = true;
}
}
//trigger collider function on exit
private void OnTriggerExit(Collider other)
{
if (other.gameObject == player)
{
playerRange = false;
}
}
}
**PlayerHealth Script:**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 100; //starting health for the player
public int currentHealth = 100; //current health
public Slider healthSlider; //references the health slider ui
// public AudioClip audioClip;
public Image damageImage; //references the flashing ui image when damage is taken
public float flashSpeed = 3f; //speed of flashing ui image
public Color flashColour = new Color(1f, 0f, 0f, 0.1f); //colour red for the damage image
Animator anim; //references the animator
//AudioSource playerAudio;
PlayerMovement playerMovement; //references player movement
bool isDead; //true if player is dead
bool damaged; //true if player is taking damage
// Start is called before the first frame update
void Awake()
{
anim = GetComponent();
// playerAudio = GetComponent();
playerMovement = GetComponent();
currentHealth = startingHealth;
}
// Update is called once per frame
void Update()
{
if (damaged)
{
damageImage.color = flashColour;
}
else
{
damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damaged = false;
}
void Death()
{
isDead = true;
anim.SetTrigger("Die");
//playerAudio.clip = deathClip;
//playerAudio.Play();
playerMovement.enabled = false;
}
public void TakingDamage(int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
//playerAudio.Play();
if (currentHealth <= 0 && !isDead)
{
Death();
}
}
public int GetCurrentHealth()
{
return currentHealth;
}
}
↧