Tuesday, September 12, 2023
HomeSample Page

Sample Page Title

The complete hp = 3, so it repeats the code 3 instances (the console reveals the log 3 instances).

It looks like you have misinterpreted how the code works. There’s nothing within the code that you’ve got shared that might repeat based mostly on the quantity of HP that the enemy has. In different phrases, if the HP is 3 and also you’re seeing “DamageENEMYYYYYYY” 3 instances in a console, it is a coincidence.

The code you have shared finds all colliders in a round space, will get the EnemyHP part from every collider, and applies harm to every. If the message “DamageENEMYYYYYYY” seems within the console 3 instances, that signifies that Physics.OverlapCircleAll() discovered 3 totally different enemies. You could make this extra apparent by together with details about the enemy in your console output:

foreach(Collider2D enemy in hitEnemies)
    Debug.Log($"Apply {harm} harm to {enemy}", enemy);

Here, we go an interpolated string to Debug.Log(); throughout the interpolated string, we are able to embrace variables inside braces, e.g. {harm} will output the worth of the harm variable.

Notice that I included a second argument to Debug.Log(), the enemy variable. The second argument is named the “context”. If the worth we go for the context is a GameObject or part, clicking on the log message within the Unity Editor will choose that GameObject/part. This makes it a lot simpler to rapidly discover the item {that a} log message is expounded to. In this case, for those who click on on the log message that claims “Apply […] harm to […]”, it should choose the enemy that the harm was utilized to.

Your present code has a design flaw – it should throw an error if there are any colliders within the round space that wouldn’t have the EnemyHP part. Your code ought to seem like this:

foreach(Collider2D collider in hitEnemies)
    var enemy = collider.GetComponent<EnemyHP>();
    //this prevents an error if the collider would not have the EnemyHP part
    if (enemy != null) {
        Debug.Log($"Apply {harm} harm to {enemy}", enemy);



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments