Melee combat, sphereoverlap not working

Hi guys,

so, on my quest to learn unity i decided to make a 2d mini rpg… And after doing some basic scripting and movement i decided i wanted to try making a basic melee system but it doesn’t work past the Debug.Log(“Step 1”) even if i’m standing right next to a cube with a Enemy health Script with the “damageDeal” function in it. Why?
what did i do wrong

Here it is:

var dmg : int = 10;
var Cooldown : float = 0.5;
var Attacktimer : float = 0;
var Range : float = 20;
var dist : float;
function Update () {
Attacktimer += Time.deltaTime;

	if (Input.GetMouseButtonDown(0))
	{
	Debug.Log("Step 1");
		// animation.Play("Attack");
		Attacktimer = 0;
		
  	  var collisions : Collider[] = Physics.OverlapSphere(transform.position, Range);
  	  for (var hit : Collider in collisions) {
   	 
   	   
   	    if (hit && hit.tag == "Enemy"){ 
   	     	
    	    
    	    dist = Vector3.Distance(hit.transform.position, transform.position);
    	    
    	    Debug.Log(dist);
    	    Debug.Log("Step 2");
    	    if (dist <= Range)
    	    {
    	    
    	    hit.gameObject.SendMessage("damageDeal", dmg);
    	    
    	    }
	
		}
	  }
	}

}

Sorry if this is something obvious, im new :wink:

OH! and thanks in advance!!

  1. Do not use capital letters when starting variables, its confusing to read, they are used for functions and classes. As you had a Range variable, there is also a Range() function. I changed all of them for you.

  2. Most important. You have if(HIT && hit.tag == …whatever…){ What is hit supposed to be here?

  3. Havent tested this tho, but think it will work now, if not let me know

    var dmg : int = 10;
    var cooldown : float = 0.5;
    var attackTimer : float = 0.0;
    var range : float = 20.0;
    var dist : float;

function Update () {
Attacktimer += Time.deltaTime;

    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
    Debug.Log("Step 1");
       // animation.Play("Attack");
       attacktimer = 0;
 
      var collisions : Collider[] = Physics.OverlapSphere(transform.position, range);
      for (var hit : Collider in collisions) {
 
 
        if (hit.tag == "Enemy"){ 
 
 
            dist = Vector3.Distance(hit.transform.position, transform.position);
 
            Debug.Log(dist);
            Debug.Log("Step 2");
            if (dist <= range)
            {
 
            hit.gameObject.SendMessage("damageDeal", dmg);
 
            }
 
       }
      }
    }
 
}