How to check who shot whom

I have a game where 25 agents are fighting against 25 other. But I have also a problem that I don’t know how to check wich enemy shot the other one. I am instantiating bullets and the bullets have a tag “bullet” for the 25 agents and “counterbullet” tag for the other 25. And also use one bullet script for the 25 agents and one script for the other 25. Do you know how I could do a check?

How are we supposed to help you if you don’t provide your current code?

A simple way to do this is to inject a reference of the shooter into the bullet somehow. I’ve done this when the bullet is instantiated by the Weapon class which is responsible to shoot. The reference of the Shooter is injected by the PlayerController at Start.

// PlayerController
public Weapon Weapon;

private void Start()
{
    Weapon.Shooter = this;
}

private void Update()
{
    if( Input.GetMouseButtonDown(0))
        Weapon.Shoot();
}

// Weapon
public PlayeController Shooter { get ; set ;}

public Bullet BulletPrefab;

public void Shoot()
{
    Bullet bullet = Instantiate( BulletPrefab );
    bullet.Shooter = Shooter;
}

// Bullet
public PlayeController Shooter { get ; set ;}

private void OnCollisionEnter( Collision collision )
{
    PlayerHealth playerHealth = collision.transform.GetComponentInParent<Health>();
    if( playerHealth != null )
    {
        playerHealth.Hurt( damages, Shooter );
    }
}

// PlayerHealth
public void Hurt( int damages, PlayerController shooter )
{
    Debug.Log("The player " + shooter.name + " has hurt me for " + damages + " HP" ) ;
}

I think i got your question , you have 1 script using on 50 objects, in 2 25 25 group with same tag names …
i think you can get name of gameobjects … i dont it think it matters that all 50 gameobject names are different … u can simply use a debug to get names of gameobject from which ray cast is initiated and which game object got hit
hit.collider.gameObject.name;

@Hellium I have the class EnemyController and EnemyBulletController:

public class EnemyBulletController : MonoBehaviour {

    public float speed;


    [HideInInspector]
    /// <summary>
    /// The associated agent.
    /// This will be set by the agent script on Initialization. 
    /// Don't need to manually set.
    /// </summary>
	EnemyController agent;
    static Animator anim;
    GameManager instanceGM;


    private void Awake()
    {
        instanceGM = GameManager.instance;
        agent = EnemyController.instance;

        anim = instanceGM.player.GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
        Destroy(gameObject, 2); //8
    }


    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "counterAgent")
        {
            try
            {

                col.gameObject.GetComponent<PlayerController>().GetGameManager().PlayerIsDead();

            }
            catch(Exception e)
            {
                Debug.Log(col.gameObject.name);
            }
        }


        if (col.gameObject.CompareTag("wall"))
        {
            Destroy(gameObject);
        }

       
    }

And this in EnemyController I instantiate the bullet:

        if (timeBtwShots <= 0)
        {
            EnemyBulletController newBullet = 
                Instantiate(bullet, firePoint.position, firePoint.rotation) 
                as EnemyBulletController;

            timeBtwShots = startTimeBtwShots;
            newBullet.speed = bulletSpeed;
        }
        else
        {
            timeBtwShots -= Time.deltaTime;
        }

How can I change my code so that I can detect which agent shot the player

@Hellium So I did like you said and it is working. The code I have send you was from the main game where I have a player. I alsoo have a unity project where I train the agents. And the code looks like this now:

if (col.gameObject.tag == "counterAgent")
{
    try
    {
        //Debug.Log("Bullet shot by: " + agent.name,agent);
        col.gameObject.GetComponent<CounterEnemyController>().GetGameManager().CounterAgentDead(col.gameObject,agent.name);
    }
    catch(Exception e)
    {
        Debug.Log(col.gameObject.name);
    }
}

And in GameManager I do this:

   public void AgentDead(GameObject opponent, string killedBy)  
    {
        if (listOfAgent.Contains(opponent))
        {
            agentsDeadsPerRound++;
            scoreCounterEnemy.IncreaseScore();

            opponent.GetComponent<EnemyController>().Done();


            for (int i = 0; i < listOfCounterAgent.Count; i++)
            {
                if (listOfAgent*.name == killedBy)*

{
listOfAgent*.GetComponent().AddReward(1.0f);*
break;
}
}
}
The code enters the catch block everytime when I use agent.name. I get this output:
System.NullReferenceException: Object reference not set to an instance of an object
at EnemyBulletController.OnTriggerEnter (UnityEngine.Collider col) [0x00039]
Do you maybe know why it is like this? And it is not entering this if statement: if (listOfAgent*.name == killedBy)*
Oh sorry my bad, it is entering the if statement. At first I placed a debug.log after the addReward() statement. And there was no output. Then I placed it before the the statement and it worked. It is a strange behaviour.

@Hellium Yes you are absolutely right! But I still get the error. Could the null refence appear because I set the EnemyController agent to public and it is awaiting an instance?
I also got the error, when I did not pass the agent.name parameter to gameManager. When I did Debug.Log(agent.name) I also got the null reference exception

Could it be that when the bullet touches the agent it gets destroyed. When it is destroyed it does not have the instance anymore

@Hellium This is the EnemyBulletController code:

using UnityEngine;
using System.Collections;
using System;

public class EnemyBulletController : MonoBehaviour {

    public float speed;

    [HideInInspector]
	public EnemyController agent;

    static Animator anim;
    GameManager instanceGM;


    private void Awake()
    {
        instanceGM = GameManager.instance;
        //agent = EnemyController.instance;

        anim = instanceGM.player.GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
        //Destroy(gameObject, 2); //8
    }


    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "counterAgent")
        {
            try
            {
                //Debug.Log("Bullet shot by: " + agent.name);
                col.gameObject.GetComponent<CounterEnemyController>().GetGameManager().CounterAgentDead(col.gameObject,agent.name);
                Destroy(gameObject);
            }
            catch(Exception e)
            {
                Debug.Log(e);
            }
        }


        if (col.gameObject.CompareTag("wall"))
        {
            Destroy(gameObject);
        }
       
    }


}

And this is the CounterEnemyBulletController:

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

public class CounterEnemyBulletController : MonoBehaviour {
    public float speed;


    [HideInInspector]
	public CounterEnemyController agent;

    static Animator anim;
    GameManager instanceGM;


    private void Awake()
    {
        instanceGM = GameManager.instance;
        //agent = CounterEnemyController.instance;

        anim = instanceGM.player.GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
        Destroy(gameObject, 2); //8
    }


    void OnTriggerEnter(Collider col)
    {

        if (col.gameObject.tag == "agent")
        {
            try
            {
                //Debug.Log("Bullet shot by: " + agent.name);
                col.gameObject.GetComponent<EnemyController>().GetGameManager().AgentDead(col.gameObject,agent.name);

            }
            catch(Exception e)
            {
                Debug.Log(e);
            }
        }
            

       
        if (col.gameObject.CompareTag("wall"))
        {
            Destroy(gameObject);
        }

    }


}