-4

So my game is a 2D top down movement game and my script does make my enemy attack but it constantly loops the attack animation because I obviously don't know what EXACTLY to put code wise to make the enemy attack when in range of the player to do damage instead of letting him constantly loop. Also i seem to be getting an error when i get close to my enemy as of right now it says

NullReferenceException: Object reference not set to an instance of an object EnemyCombat.Attack () (at Assets/EnemyCombat.cs:36) EnemyCombat.Update () (at Assets/EnemyCombat.cs:25)

Also, Here is the EnemyCombat script

    enemy attausing System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyCombat : MonoBehaviour
    {
        public Animator animator;

        public Transform AttackPoint;

        public float attackRange = 0.5f;

        public LayerMask enemyLayers;

        public int attackDamage = 5;

        public float attackRate = 2f;
        float nextAttackTime = 0f;

        // Update is called once per frame
        void Update()
        {
            if (Time.time >= nextAttackTime)
            {    
                Attack();
                nextAttackTime = Time.time + 1f / attackRate;    
            }
        }

        void Attack()
        {
            animator.SetTrigger("Attack");
            Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayers);
            foreach (Collider2D enemy in hitEnemies)
            {
                enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
            }
        }

        void OnDrawGizmosSelected()
        {
            if (AttackPoint == null)
                return;

            Gizmos.DrawWireSphere(AttackPoint.position, attackRange);
        }
    }
Donut
  • 110,061
  • 20
  • 134
  • 146
ItsDamu
  • 11
  • 3
  • well sounds like either `animator`, `AttackPoint` or `GetComponent` is `null` (we don't know what line exactly is 36) ... have you tried [Debugging your code](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) ? – derHugo Nov 11 '21 at 10:59

2 Answers2

1

To fix your endless attack loop:

// Update is called once per frame
void Update()
{
    if (attackRate >= nextAttackTime) /* checks that nextAttackTime is less than or equal to the attackRate */
    {    
        Attack();
        nextAttackTime = Time.deltaTime * 5f; // adds 5 seconds to nextAttackTime   
    }
    else
    {
        nextAttackTime -= Time.deltaTime; /* if nextAttackTime is greater than the attackRate, subtract one from nextAttackTime. this only happens once per second because you use Time.deltaTime */
    }
}
cireneirbo
  • 133
  • 7
  • Also, you might want to rename `nextAttackTime` to `nextAttackTimer` just so you can understand it a little easier when you look at again in a few months – cireneirbo Nov 10 '21 at 22:10
0

From that NullReference Error it looks like the major problem you're having is that there is no actual point in the code or in the game hierarchy that you are telling your script which gameObject it is referring to.

Line 36 is trying to retrieve that information with .GetComponent<Enemy()>, so you need to provide that reference.

You could do this in the script fairly easily by creating a public variable that you can drag the enemy gameObject into in your hierarchy in Unity.

Try something like: public GameObject enemyObject;

This will create a variable in the script visible in the hierarchy when you select the script which you can drag the appropriate gameObject into.

There might need to be some adjustments to it because I can't see the rest of your code, but this seems to be the issue.

Another option would be trying to manually adding it in:

void Start()
    {
        GameObject enemyObject = gameObject.GetComponent<Enemy>();
    }

this is taken from Unity Scripting Documentation

cireneirbo
  • 133
  • 7
  • for some reason the gameobject code you gave is not giving me a space to drag a gameobject into. I know exactly what you mean but for some reason typing that doesnt add a slot to the script for a gameobject, any reason why this could be? – ItsDamu Nov 10 '21 at 19:04
  • That is very odd! I'm sure it has you irritated by now. I'll add another option in my original post. This comment section is horrible for formatting. – cireneirbo Nov 10 '21 at 19:32
  • got it to work! at least to hurt and do damage to my main player lol. My monster will still attack every second he can, any solution for that? – ItsDamu Nov 10 '21 at 21:47
  • `GameObject enemyObject = gameObject.GetComponent();` makes no sense ... what is this supposed to do? a `GameObject` is not a component so you can't use it for `GetComponent` ... – derHugo Nov 11 '21 at 10:52