🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

NullReferenceException

Started by
3 comments, last by frob 3 years, 5 months ago

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

Ícono de validado por la comunidad

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

Ícono de validado por la comunidad

Ícono de validado por la comunidad

Advertisement

First question: does everything in the enemy layer have an EnemyHealth component?

Second question: could you specify which tutorials you used? (I would have thought that tutorials would cover this sort of thing)

@RulerOfNothing I have all the enemy's gameobjects, inside one, so I guess it will apply to all enemy components. I have seen two different people's tutorials, one explaining enemy animations, attack, mobility, and tracking to the player and the other tutorial is about the player's life and attack to deal damage to the enemy. I have tried to put the two together. Thank you very much for your answer partner. I was excited haha

This is a place to leverage your debugger.

foreach (Collider2D enemy in hitEnemies)

{
    enemy.GetComponent<EnemyHealth>().TakeDamage(attackDamage);
}

Most likely the collider being processed doesn't have an EnemyHealth component attached.

There are only a few other ways you could get the error there, and each is unlikely. You could get it if there was a null entry in the collection, but since you got it from a Physics2D.OverlapCircleAll() call, that's unlikely unless you're mucking about with concurrent code. If you were doing some advanced type marshalling it is also possible to corrupt an object so badly that your TakeDamage() function could give the error, but that's highly unlikely.

You can also leverage the logging system in the editor.

An easy option that will spam your debug log is to put something like this immediately before it:

Debug.Log("Attacking collider " + enemy.name, enemy);

Less spamming but assumes you already know the source of the error:

foreach (Collider2D enemy in hitEnemies)

{

   Debug.Assert( enemy != null, enemy );

   

   EnemyHealth enemyHealth = enemy.GetComponent<EnemyHealth>();

   if( !enemyHealth ) 

   {

      Debug.LogError("Collider does not have EnemyHealth component. " + enemy.name, enemy);
   

   } else {
      enemyHealth.TakeDamage(attackDamage);
   }
}

This topic is closed to new replies.

Advertisement