Help! Camera movement using empty GameObjects and UI Buttons.

Hi!
I’m quite new with unity and C# and just started my first game after doing some online courses. I’m currently struggling with a problem and have been the past days. I have yet to find a similar problem and/or solution, sorry if I missed something.

What I’m essentially trying to achieve is moving the Main Camera to different GameObjects by collecting their transform components. I have attempted to do this by placing empty GameObjects in the world, attaching both the Main Camera and the empty GameObject to a GameObject with a Button component. The script tied on my Main Camera is:

public class MoveCamera : MonoBehaviour
{
    Camera mainCamera;
    ZoneButton gameObjectCamera;

    void Start()
    {
        mainCamera = FindObjectOfType<Camera>();
        gameObjectCamera = FindObjectOfType<ZoneButton>();
    }

    public void MoveCameraLocation()
    {

        Vector3 currentPos = new Vector3(mainCamera.transform.position.x, mainCamera.transform.position.y, mainCamera.transform.position.z);
        Vector3 newPos = gameObjectCamera.ReturnTransform();
        transform.position = Vector3.MoveTowards(currentPos, newPos, 160);
    }
}

The Main Camera is then attached to the button like this, where each of the blue square in the image has its own unique “Zone Transform” which is just empty GameObjects used for their transform component.

The “Zone2” is the empty GameObject from which ZoneButton calls the transform component, like this:

public class ZoneButton : MonoBehaviour
{
    [SerializeField] GameObject zoneTransform;

    public Vector3 ReturnTransform()
    {
        Vector3 zonePos = zoneTransform.transform.position;
        return zonePos;
    }
}

What ends up happening is that only the first click works, and no matter which of the buttons I press it always goes to the same location. I have dragged and dropped all the components correct, everything within unity appears to work. It think I’m missing something in the code so that only one of the “Zone Transform” GameObjects works.

Solvet it, attached this script to the main camera:

public class CameraMovement : MonoBehaviour
{
    public Transform startTransform;
    [SerializeField] float time = 1f;

    void Awake()
    {
        SnapToPosition();
    }

    public void SnapToPosition()
    {
        transform.position = startTransform.position;
        transform.rotation = startTransform.rotation;
    }

    public IEnumerator MoveToPosition(Transform newZone)
    {
        if (newZone == transform)
        {
            yield break;
        }
        Vector3 startPosition = transform.position;
        Quaternion startRotation = transform.rotation;
        Vector3 endPosition = newZone.position;
        Quaternion endRotation = newZone.rotation;

        float elapsedTime = 0;
        while (elapsedTime < time)
        {
            float a = elapsedTime / time;
            transform.position = Vector3.Lerp(startPosition, endPosition, a);
            transform.rotation = Quaternion.Slerp(startRotation, endRotation, a);

            yield return new WaitForEndOfFrame();
            elapsedTime += Time.deltaTime;
        }
    }
}

And this to the empty GameObjects placed around in the world with a button component:

public class ZoneNavigator : MonoBehaviour
{
    public CameraMovement cameraMover;
    public Button buttons;

    void OnEnable()
    {
        buttons.onClick.AddListener(delegate { cameraMover.StartCoroutine(cameraMover.MoveToPosition(transform)); });
    }
}