How do I get one object to move to (the closet) another object?

Hello, I am struggling to get my objects to do what I want them to do. My ultimate goal is to have two groups, Ally and Enemy that are fighting eachother. However, after nearly a week of seeking help, reading, and watching any related thing I can find, I have made very little progress.

Right now I am trying to get Ally to seek-out the nearest Enemy. The method that I have had the most success is was telling one of my objects to seek out others with a certain tag. This worked, but not well. They would rush toward one Enemy, kill it, and then idle. Other Allyobjects would spawn and then rush to another one, kill it, and go idle themselves.

The code for this is:

using UnityEngine;
using System.Collections;

public class AlliedAttack : MonoBehaviour
{
    public float timeBetweenAttacks = 0.5f;
    public int AlliedAttackDamage = 10;

    RaycastHit shootHit;
    PlayerShooting playerShooting;
    Animator anim;
    GameObject enemy;
    PlayerHealth playerHealth;
    EnemyHealth enemyHealth;
    bool EnemyInRange;
    float timer;


    void Awake()
    {
        enemy = GameObject.FindGameObjectWithTag("Enemy");
        enemyHealth = enemy.GetComponent<EnemyHealth>();
        anim = GetComponent<Animator>();
    }


    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == enemy)
        {
            EnemyInRange = true;
        }
    }


    void OnTriggerExit(Collider other)
    {
        if (other.gameObject == enemy)
        {
            EnemyInRange = false;
        }
    }


    void Update()
    {
        timer += Time.deltaTime;

        if (timer >= timeBetweenAttacks && EnemyInRange && enemyHealth.currentHealth > 0)
        {
            Attack();
        }

    }


    void Attack()
    {
        timer = 0f;

        if (enemyHealth.currentHealth > 0)
        {
            enemyHealth.TakeDamage(AlliedAttackDamage, shootHit.point);
        }
    }
}

//
//
//

using UnityEngine;
using System.Collections;

public class AlliedMovment : MonoBehaviour
{
    Transform enemy;
    EnemyHealth enemyHealth;
    UnityEngine.AI.NavMeshAgent nav;


    void Update()

    {
        enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
        nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
        nav.SetDestination(enemy.position);
    }
}

//
//
//
My second attempt was to use Vector3.MoveTowards. All I have managed to do with this one is to get my objects to run from one side of the room to the other and sit there.
//
//
using UnityEngine;
using System.Collections;

public class AlliedMovement2 : MonoBehaviour
{

    public Transform target;

    public float speed;

    void Update()
    {
         transform.position = Vector3.MoveTowards(transform.position, target.transform.position * Time.deltaTime);
         float step = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }
}

//
//
//

Finally, I tried this method:

//
//

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

public class AlliedMovement2 : MonoBehaviour
{
    public Transform enemy;
    public Transform myTransform;

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update ()
    {
        transform.LookAt(enemy);
        transform.Translate(Vector3.forward * 5 * Time.deltaTime);
    }
}

It does the exact same thing as my attempt #2, but instead of crossing the entire room, they rush toward the 0, 0 , 0 point and…act strange.

At no point did I manage to figure out how to make my objects find the nearest enemy, either. They sort of just pick the oldest one to spawn and charge it. Could anyone please suggest what I could try or point out what I’m doing wrong?

You can sort the list of enemies (after you got it using tags) by distance using Comparers. Then make your allies go for the first element in the list (aka the closest enemy). After the enemy dies, remove him from the list, resort and make them run for the first element again. Repeat until all enemies are dead (GetGameObjectsWithTag returns an empty list).

The easiest, although not always giving the most natural looking behaviour, it to use a finite state machine, just like the Animator state machines.
Your entity (ally or enemy) will always be in one state and only one, and in each state, it has conditions to transition/switch to other states.

The most common way to define the states is to use enums.

public enum BehaviourState {Idle, LookingAround, Patrolling, Chasing, Fighting, Fleeing, Dying, Dead}
private BehaviourState behaviourState;

Then, from Update(), or FixedUpdate(), you can use a switch statement, to trigger actions, and look for conditions to transition to other states.

void Update()
{
    switch (behaviourState)
    {
        case BehaviourState.Idle:
            break;
        case BehaviourState.LookingAround:
            LookAround();
            break;
        case BehaviourState.Patrolling:
            Patrol();
            break;
        case BehaviourState.Chasing:
            Chase (lastKnownTarget);
            break;
            // and so on
    }
}

Chase (Entity target)
{
    // if target is too far and too fast, look for another one
    if (Vector3.Distance (target.transform.position, transform.position) > givupDistance && target.speed > speed)
        behaviourState = BehaviourState.LookingAround;
}