Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question 2D Game Scripting Issue

Discussion in 'Scripting' started by TheDemandinPath, Mar 8, 2023.

  1. TheDemandinPath

    TheDemandinPath

    Joined:
    Feb 14, 2023
    Posts:
    2
    Hello! I am in a Game Design class that makes us have to make prototypes. I am trying to make a combo system and am making a basic combat system first. I am trying to make it to where the enemy is registered as being hit and that their health goes down. However I keep getting the issue:
    NullReferenceException: Object reference not set to an instance of an object. Here is my code for the player when it comes to the attacks:
    // Detect enemy in range of attack
    Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange);//enemyLayers);
    Debug.Log(hitEnemies.Length);

    // Damage them
    foreach(Collider2D enemy in hitEnemies)
    {
    Debug.Log(enemy);
    Enemy EnemyReference = enemy.GetComponent<Enemy>();
    EnemyReference.TakeDamage(attackDamage);
    }
    void OnDrawGizmosSelected()
    {
    if (attackPoint == null)

    Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    }
    And here's the enemy's script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Enemy : MonoBehaviour
    {
    public int maxHealth = 100;
    public int currentHealth;

    // Start is called before the first frame update
    void Start()
    {
    currentHealth = maxHealth;
    }

    public void TakeDamage(int damage)
    {
    currentHealth -= damage;

    Debug.Log("Enemy hit!");

    // Play hurt animation

    if(currentHealth <= 0)
    {
    Die();
    }
    }

    void Die()
    {
    Debug.Log("Enemy has died!");
    // Die animation

    // Disable the enemy
    }
    }

    Any help would be much appreciated! Thank you!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    First off, you should use code tags when you post code. That will give the code line numbers and color code it. It's much easier for us to read and understand that way.

    There's a pinned thread that teaches you how to find null reference errors:
    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    If you still can't figure it out after doing that, please repost your code with tags and tell us what line number the error says it's coming from.
     
    RadRedPanda likes this.
  4. TheDemandinPath

    TheDemandinPath

    Joined:
    Feb 14, 2023
    Posts:
    2
    It is on line 93 aka the one where I list EnemyReference
     
  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Well you never even check if the colliders you hit have enemy components, you just try to get it. If your game has a wall, it probably has a collider, and I'm guessing it wouldn't have an enemy component, so why would you expect everything in your OverlapCircle to have an enemy component?
     
  6. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    You're using Physics2D.OverlapCircleAll without a layer mask. I see you've commented out a variable called enemyLayers. If you want it to only find colliders in the enemy layer, determine which layer number that is and stick it in the variables for OverlapCircleAll. Otherwise, as RadRedPanda said, you're picking up every collider within that circle and some of them probably aren't enemies and don't have the Enemy script.
    https://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircleAll.html
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    We don't get line numbers because you've posted code as plain text. As already requested above, please edit your post to use code-tags.