Is it possible to find an object by variable?

Hi everybody!
I have a ship in my game it has port and right board situated guns. Guns search for the targets in array with tag
GameObject targets = GameObject.FindGameObjectsWithTag(enemyTag); Tag = Enemy
But I recenty found, that in a condition whenmy ship is attacked not from 1 side bu t from both sides guns still looking for the only nearest and half of guns “shoot” through the ship.

I haven’t managed to come up with anything but for splitting the tag by parts like Enemy+PortBoard etc. Taking in consideration that there are 300 ships in the game with standard behaviour - it is not possible to make 180 000+ tags. So it must be a variable of type String or a number composed automatically. But I have not found a way to form the enemy array by a variable.

Of course it is wrong but I need something like this:
GameObject targets = GameObject.FindGameObjectsWithString(Friend1);
GameObject targets = GameObject.FindGameObjectsWithVar(1000001);

but GameObject targets = GameObject.FindGameObjectsWithTag(enemyTag); using that you already have the variable? After that you could do whatever you like with them, either shoot or check angles, distances etc to decide whether you what to shoot.

Have you considered using maybe a collider trigger to see if enemy ship is in range of specific gun? That way you can have different guns with different attack range and as soon as they enter the collider trigger, you have the enemy variable.

void OnTriggerEnter(Collider coll)
{
    if(coll.tag == "Enemy")
    {
        //hurt this guy bad
    }
}

Something like that, or maybe I am completely misunderstanding the question.

Hello @DimaDimov , There is GameObject.Find(string); that search a gameobject in the hierarchy by name :slight_smile: Unity - Scripting API: GameObject.Find

You can use FindObjectsofType, that will search on the bases of the component or script attached to the game object, you can attach script and then find all those object which has this script attached to them

You could check in the enemy script, on which side of the ship it is and then let it assign itself to your ships array or you could raycast against enemies and use a loop function to check where they are and then assign them to the right gun as target.

Thanks everybody who answered. I tried to solve the probleme https://vimeo.com/380812824 by tags. But nothing happens. Guns still aim the oppositr board aims. Here is the script. The best way to cope with probleme is to switch the script off. BUT EVEN THIS DOES NOT WORK. The guns aim with the script switched off.

protected IEnumerator FindClosestTarget()
{
    while (true)
    {
        Transform closest = null;
        GameObject[] targets = GameObject.FindGameObjectsWithTag(«Enemy2»);
        float distance = sqrVisionRadius;
        foreach (GameObject go in targets)
        {
            Vector3 diff = go.transform.position - transform.position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = go.transform;
                distance = curDistance;
            }
        }
        target = closest;
        nearest = target.name;
        yield return new WaitForSeconds(searchTimeDelay);
    }
}