I have 1 week into making games, and I have seen tutorials on youtube in which I made two scrpits. One of mobility next to the enemy's attack (only animation) and I tried to add a scripts so that the enemy has life and can receive damage from the player but in the scripts I get nullreferencexception in line 44 of the following scripts: enemy.GetComponent<EnemyHealth>().TakeDamage(attackDamage);
complete code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
public Animator animator;
public Transform attackPoint;
public LayerMask enemyLayers;
public float attackRange = 0.5f;
public int attackDamage = 40;
public float attackRate = 2f;
float nextAttackTime = 0f;
void Update()
{
if (Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.G))
{
Attack();
nextAttackTime = Time.time + 1f / attackRate;
}
}
}
void Attack()
{
animator.SetTrigger("Ataque1");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<EnemyHealth>().TakeDamage(attackDamage);
}
}
private void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
{
}
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}
any way to associate the scripts and make them work?
sorry for the narrative, I used a translator