OnTriggerEnter gets called only once

Hey guys

I try to develop a simple 2d game by unity. It’s about a little knight, who uses a sword as a melee attack. So I tried to use the function OnTriggerEnter whenever an enemy is in range and you press space. To test it, I wrote a little text in the Debug.Log() Method. Unfortunatly, it gets called only once regardless how often I press space. Although, it works fine while I run into the enemy or choose the other side to attack it.

Do you have any ideas what the problem is?

I copied both scripts down below, just in fact that you want to have a look at them

Thanks for any help in advance. I’m really desperated and annoyed to be honest

And sorry for my English.

Attack script:

public BoxCollider2D range;
private bool attacking = false;

bool pressedSpace = false;

private Animator anim;

void Start()
{
	range = gameObject.GetComponent<BoxCollider2D>();
	anim = GetComponent<Animator>();
	range.enabled = false;

}

void Update()
{

	pressedSpace = false;
	anim.SetBool("pressedSpace", pressedSpace);
	attacking = false;
	range.enabled = false;

	if(Input.GetKeyDown(KeyCode.Space) && !attacking)
		{
				attacking = true;
				range.enabled = true;
				pressedSpace = true;
				anim.SetBool("pressedSpace", pressedSpace);
		}
}

attack Trigger:

public int dmg = 30;

void OnTriggerEnter2D(Collider2D col)
{
	if(col.isTrigger != true && col.CompareTag("Enemy"))
//	col.SendMessageUpwards("Damage",dmg);
	Debug.Log("works");
}

Im pretty sure you just need to change your trigger to a stay trigger.

private void OnTriggerStay2D(Collider2D col)
{
    if(col == player)
    {
        playerIsInRange = true;
    }
}

Also you might need to use

private void OnTriggerExit2D(Collider2D col)
{
    if(col == player)
    {
        playerIsInRange = false;
    }
}

to know when the player is no longer in range. Unity - Scripting API: MonoBehaviour.OnTriggerExit2D(Collider2D)

EDIT:

Ok, you need to step back a bit and redesign the way you are doing this I think. If it was me I would do the following.

In the Unity Scene you should have:

1 Player gameobject. The Tag on the Player gameObject should be set to “Player”. This gameobject should also have a collider enabled (NOT A TRIGGER COLLIDER).

1 Player Trigger Radius gameobject. This is a child of Player gameObject.
Player Trigger Radius should have a collider set to is trigger. The Mesh Renderer should be set to inactive (turned off) on this gameobject so it is not visible at run time. Only the collider should be active acting as a proximity field for the player. IMPORTANT: Set the size of this gameobject to represent the radius of the players attack range. If an enemy collider passes into this trigger collider it will send a call to OnTriggerEnter() and OnTriggerStay. When the enemy collider exits this trigger collider a call to OnTriggerExit() is made.

1 Enemy gameobject with a Tag set to “Enemy” or whatever you like. Also, the Enemy GameObject needs a collider enabled (NOT A TRIGGER COLLIDER).

1 C#Script named Player place it onto the Player gameobject.

private PlayerTriggerRadius playerTriggerRadius;
private bool tryAttack;

private void Start()
{
    playerTriggerRadius = FindObjectOfType<PlayerTriggerRadius>();
}

private void Update()
{
    if (Input.GetKeyDown(KeyCode.Space)
    {
        tryAttack = true;
    }

    if (playerTriggerRadius.enemyInRange == true && tryAttack == true)
    {
        //PUT CODE FOR ENEMY IN RANGE ATTACK HERE.  THIS CASE WILL OCCUR WHEN SPACE IS PRESSED AND THE ENEMY IS IN RANGE.
        Debug.Log("Player just attacked and hit the enemy.");
        tryAttack = false;
    }
    else if (playerTriggerRadius.enemyInRange == false && tryAttack == true)
    {
        tryAttack = false;
    }
}

1 C#Script named PlayerTriggerRadius. Make sure you dont forget to put it onto the Player TriggerRadius gameObject.

public bool enemyInRange;

private void OnTriggerEnter(Collider col)
{
    if (col.tag == "Enemy")
    {
        enemyInRange = true;
    }
}
private void OnTriggerExit(Collider col)
{
    if (col.tag == "Enemy")
    {
        enemyInRange = false;
    }
}

NOW… If you do everything here just like this. The player will hit the enemy when they are overlapping the player trigger collider game object. Ask if you get confused about anything and I’ll try to clarify. Hope this helps.