How can I make a gate with two cubes that close/open nonstop?

using System.Collections; using System.Collections.Generic; using UnityEngine;  public class Gate : MonoBehaviour {     public GameObject[] gateParts;      // Start is called before the first frame update     void Start()     {              }      // Update is called once per frame     void Update()     {         for (int i = 0; i < gateParts.Length; i++)         {             gateParts[i].transform.position = Vector3.MoveTowards(gateParts[i].transform.position, gateParts[i + 1].transform.position, Time.deltaTime * 3f);         }     } } 

I want to make both cubes to move each other when they collide or touch each other by distance then move them far from each other. close/open the gate nonstop.