0

Hey guys I am getting this error in my unity console I want to make a 2d sword combat game. But I am not able to damage the enemy. Pls Help!

NullReferenceException: Object reference not set to an instance of an object
DoDamage.doDamage () (at Assets/Scripts/DoDamage.cs:16)
PlayerAttack.SwordCombat () (at Assets/Scripts/PlayerAttack.cs:54)
PlayerAttack.Update () (at Assets/Scripts/PlayerAttack.cs:25)

My PlayerAttack script This script is attached to player All the public GameObjects in this script are filled

public class PlayerAttack : MonoBehaviour {

    public Animator anim;
    private Vector2 movement;
    private Vector2 sprint;
    private float doubleClickTime = 0.2f;
    private float lastClickTime;
    [SerializeField]
    public bool punched;
    public bool isArmed = false;
    private GameObject sword;

    void Start(){
        movement = gameObject.GetComponent<PlayerMove>().movement;
        sprint = gameObject.GetComponent<PlayerMove>().sprinting;
    }

    void Update () {
        if(isArmed){
            sword = gameObject.GetComponent<PickupWeapon>().sword;
            SwordCombat();
        }else{
            HandCombat();
        }
    }

    public void HandCombat(){
        if(Input.GetKeyDown(KeyCode.Mouse0)){
            float timeSinceLastClick = Time.time - lastClickTime;
            if(timeSinceLastClick < doubleClickTime && punched){
                anim.SetTrigger("DoublePunch");
                punched = false;
            }else{
                anim.SetTrigger("Punch 1");
                punched = true;
            }
            lastClickTime = Time.time;
        }
    }

    public void SwordCombat(){
        if(Input.GetKeyDown(KeyCode.Mouse0)){
            float timeSinceLastClick = Time.time - lastClickTime;
            if(timeSinceLastClick < doubleClickTime && punched){
                anim.SetTrigger("DoubleSwordAttack");
                sword.GetComponent<DoDamage>().doDamage();
                punched = false;
            }else{
                anim.SetTrigger("SwordAttack");
                sword.GetComponent<DoDamage>().doDamage();
                punched = true;
            }
            lastClickTime = Time.time;
        }
    }
}

My DoDamage script

This script is responsible to damage the enemy

public class DoDamage : MonoBehaviour {

    public Transform attackPoint;
    public float attackRange = 0.5f;
    public LayerMask enemyLayers;
    private GameObject damageTaker;

    public void doDamage(){
        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

        foreach(Collider2D enemy in hitEnemies){
            enemy.gameObject.GetComponent<Control>().TakeDamage(50f);
        }
    }

    void OnDrawGizmosSelected(){
        if(attackPoint == null){
            return;
        }
        Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    }

}

My Control script

This script is attached to enemy and contain its key features

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Control : MonoBehaviour {

    public float speed = 8f;
    public float maxHealth = 100;
    float currentHealth;

    void Start(){
        currentHealth = maxHealth;
    }

    public void TakeDamage(float damage){
        currentHealth -= damage;
        if(currentHealth <= 0){
            Die();
        }
    }

    public void Die(){
        Debug.Log("Enemy Died!");
    }

}

I have tried number of solutions. I have wrote this code using one of the brackeys tutorial on 2D combat system. It has the same code as mine but I am getting errors.

I dont know what does the error mean and not getting a perfect solution. Pls Help!

  • Is `attackPoint` referenced? What if you hit something that doesn't have a `Control` component on the same `GameObject` as the collider? – derHugo Apr 13 '21 at 10:57
  • I have referenced everything and the gamobject has the control script too – The Indian Apr 14 '21 at 05:38

0 Answers0