Problem with multiple enemy script.

Hi, I’m recent starting to use Unity and C# and I have a little issue. That issue is that when an enemy deals damage to the player it counts the damage of all enemies on the screen, as if they where hitting you at the same time. I hope someone can help me soon as possible, I’ll paste and upload my script to the thread so someone can help me to correct it. (Sorry for my English, it’s a bit bad)

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

public class IA_Enemy : MonoBehaviour
{
    public Transform player;
    UnityEngine.AI.NavMeshAgent enemyNav;
    private bool inside = false;
    public Animator enemyAnim;
    public GameObject self;
    public PlayerManagement PlayerManagement;
    void Start()
    {
        enemyNav = GetComponent<UnityEngine.AI.NavMeshAgent>();
        enemyAnim = this.GetComponent<Animator>();
    }

    void OnTriggerStay(Collider other)
    {
        if (other.tag == ("Player"))
        {
            inside = true;

            if (PlayerManagement.dmgTemp > 1.2f)
            {
                PlayerManagement.dmgTemp = 0;
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == ("Player") && PlayerManagement.dmgTemp > 1.2)
        {
            PlayerManagement.dmgTemp = 0;
            PlayerManagement.cameraAnim.Play("CameraHitAnim");
            enemyNav.destination = this.transform.position;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if(other.tag == ("Player"))
        {
            inside = false;
        }
    }

    void Update()
    {

        if (PlayerManagement.dmgTemp < 0.2f)
        {
            PlayerManagement.ActualLife -= 1f;
            enemyAnim.Play("Attack");
            enemyNav.destination = this.transform.position;
        }

        if (!inside && PlayerManagement.dmgTemp > 0.8f)
        {
            enemyNav.destination = player.position;
        }

        if (inside)
        {
            enemyNav.destination = this.transform.position;
        }

        var lookPos = player.position - transform.position;
        lookPos.y = 0;
        var rotation = Quaternion.LookRotation(lookPos);
        self.transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
    }
}

7937596–1014694–IA_Enemy.cs (1.93 KB)

Simplify it down to just 2 enemies, give them unique names so you can differentiate them, then do some testing, keeping this in mind to gain insight into what is happening:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, consider using Debug.DrawRay() or Debug.DrawLine() to visualize things like raycasts or distances.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Thanks for the advice, I’ll try to do that

Finaly I solved it! Again, thank you very much friend!

1 Like