How do i pick a random tree and go towards it?

In my game i have a AI which should find a random tree and go towards it and cut it down
I am new to unity and can’t figure it out myself.

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



public class GatherWood : MonoBehaviour
{
    [SerializeField] private Transform storageNodeTransform;
    [SerializeField] private GameObject[] treesToPickFrom;
    [SerializeField] private state _state;
    public float speed;
    private enum state
    {
        idle,
        goingTowardsWood,
        gatheringWood,
        goingTowardsStorage,
    }

    private void Update()
    {
        if (_state == state.goingTowardsWood)
        {
            int randomTree = Random.Range(0, treesToPickFrom.Length);
            GameObject randomTreeName = treesToPickFrom[randomTree];
            print(randomTreeName);
            MoveTo(randomTreeName);
        }else if (_state == state.idle)
        {
            return;
            //Add Idle animation here
        }
    }
    void MoveTo(GameObject moveTo)
    {
        transform.position = Vector2.MoveTowards(transform.position, , speed * Time.deltaTime);
    }
}

This is the code i managed to figure out :frowning:

Quick solution adapted to your code NOT TESTED

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

public class GatherWood : MonoBehaviour
{
    [SerializeField] private Transform storageNodeTransform;
    [SerializeField] private GameObject[] treesToPickFrom;
    [SerializeField] private state _state;
    private Transform targetTree;
    public float speed;
    private enum state
    {
        idle,
        goingTowardsWood,
        gatheringWood,
        goingTowardsStorage,
    }

    private void Update()
    {
        if (_state == state.goingTowardsWood)
        {
            if(targetTree == null)
            {
                int randomTree = Random.Range(0, treesToPickFrom.Length);
                targetTree = treesToPickFrom[randomTree].transform;
                print(randomTreeName);
            }
            MoveTo(targetTree);
        }
        else if (_state == state.goingTowardsStorage)
        {
            targetTree = null;
            MoveTo(storageNodeTransform);
            return;
        }
        else if (_state == state.idle)
        {
            return;
            //Add Idle animation here
        }
    }
    void MoveTo(Transform target)
    {
        transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    }
}

Suggested rework for more flexibility, based on some kind of State machine system

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

// Lumberjack.cs
public class Lumberjack : MonoBehaviour
{
    [SerializeField] private Transform storage;
    [SerializeField] private GameObject[] treesToPickFrom;
    [SerializeField] private float speed;
    private State state;

    public GameObject[] TreesToPickFrom => treesToPickFrom;
    public Transform Storage => storage;

    private void Awake()
    {
        state = new FindTree(this);
    }

    private void Update()
    {
        state = state.Run();
    }

    public void MoveTowards(Transform target)
    {
        transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    }

    public float DistanceFrom(Transform target)
    {
        return Vector3.Distance(transform.position, target.position);
    }

    private abstract class State
    {
        private Lumberjack lumberjack;
        protected Lumberjack Lumberjack => lumberjack;

        protected State(Lumberjack lumberjack)
        {
            this.lumberjack = lumberjack;
        }

        public abstract State Run();
    }

    private class FindTree : State
    {
        private Transform targetTree;

        public FindTree(Lumberjack lumberjack) : base(lumberjack)
        {
            targetTree = PickTree(lumberjack.TreesToPickFrom);
        }

        public FindTree(Lumberjack lumberjack, Transform targetTree) : base(lumberjack)
        {
            this.targetTree = targetTree;
        }

        private Transform PickTree(GameObject[] trees)
        {
            int randomTree = Random.Range(0, trees.Length);
            return trees[randomTree].transform;
            // Pick closest one instead?
        }

        public override State Run()
        {
            Lumberjack.MoveTowards(targetTree);
            return Lumberjack.DistanceFrom(targetTree) < 1
                ? (State) new GatherWood(Lumberjack, targetTree) // Tree reached? Start gathering
                : this; // Continue moving towards
        }
    }

    private class GatherWood : State
    {
        private float gatheringStartTime;
        private Transform targetTree;

        public GatherWood(Lumberjack lumberjack, Transform targetTree) : base(lumberjack)
        {
            this.targetTree = targetTree;
            gatheringStartTime = Time.time;
            // Start animation ?
        }

        public override State Run()
        {
            return ( Time.time - gatheringStartTime ) > 5 
                ? (State) new MoveToStorage(Lumberjack, targetTree) // 5 seconds gathering wood? Go to the storage
                : this; // Continue gathering wood otherwise
        }
    }

    private class MoveToStorage : State
    {
        private Transform targetTree;

        public MoveToStorage(Lumberjack lumberjack, Transform targetTree) : base(lumberjack)
        {
            this.targetTree = targetTree;
        }

        public override State Run()
        {
            Lumberjack.MoveTowards(Lumberjack.Storage);
            return Lumberjack.DistanceFrom(Lumberjack.Storage) < 2
                ? (State) new FindTree(Lumberjack, targetTree) // Storage reached? Start moving to last wood again
                : this; // Continue moving towards storage otherwise
        }
    }
}