Randomize "GameObject.FindWithTag"

Hey! I have some code, where multiple “Miners” can collect “Pickup’s” and return it to a “base” it all works fine, after the miner returns to the base - they then go to find a new pickup, and if a miner is going for a pickup and it is taken by another miner - the miner will find a new pickup to find.

My only problem is, the miners will always go for the same pick-up until it’s been picked up, I believe it’s because “GameObject.FindWithTag” is always telling them to find the object highest in the hierarchy, is there a way to randomize which object it finds?

I will paste my entire code below, but I’m fairly certain this is the issue. Any help would be massively appreciated!

using UnityEngine;
using System.Collections;

public class minerAI : MonoBehaviour
{
    private GameObject smallPickup;
    private smallPickupInfo smallPickupInfo;
    private Vector3 targetLocation;

    private GameObject myBase;
    private Vector3 baseLocation;

    private GameObject currencyManager;
    private currencyManagement currencyManagement;

    private Vector3 currentLocation;
    

    private float speed = 2;

    private bool pickedUp = false;
    private bool target = false;

    private int smallPickupWorth = 0;

    void Awake()
    {
        smallPickup = GameObject.FindWithTag("Small Pickup");
        smallPickupInfo = smallPickup.GetComponent<smallPickupInfo>();

        myBase = GameObject.FindWithTag("Default Base");

        currencyManager = GameObject.Find("Currency_Manager");
        currencyManagement = currencyManager.GetComponent<currencyManagement>();
    }

    void Start()
    {
        if (smallPickup != null)
        {
            targetLocation = smallPickup.transform.position;
            target = true;
        } else if (smallPickup == null)
        {
            // Debug.Log("Something went wrong with the small pick-up");
        }

        if (myBase != null)
        {
            baseLocation = myBase.transform.position;
        } else if (myBase == null)
        {
           // Debug.Log("Something went wrong with myBase");
        }
       
        currentLocation = transform.position;
        StartCoroutine("MoveTowardsPickup");

    }

    void Update()
    {
        if (smallPickup == null)
        {
            target = false;
        }

        if (smallPickup != null && smallPickupWorth == 0)
        {
            smallPickupWorth = smallPickupInfo.worth;
        }
    }

     IEnumerator MoveTowardsPickup()
    {
        if (target == false)
        {
            smallPickup = GameObject.FindWithTag("Small Pickup");
            targetLocation = smallPickup.transform.position;
            target = true;
        }

        if (smallPickup != null)
        {
            yield return new WaitForSeconds(0.01f);
            transform.position = Vector3.MoveTowards(transform.position, targetLocation, (speed * Time.deltaTime));
            if (transform.position != targetLocation)
            {
                StartCoroutine("MoveTowardsPickup");
            } else if (transform.position == targetLocation)
            {
                pickedUp = true;
                // Debug.Log("I Arrived");
                StartCoroutine("ReturnToBase");
                currentLocation = transform.position;
                Destroy(smallPickup);
            }


        }
    }

    IEnumerator ReturnToBase()
    {
        yield return new WaitForSeconds(0.01f);
        // Debug.Log("Returning to base");
        transform.position = Vector3.MoveTowards(transform.position, baseLocation, (speed * Time.deltaTime));
        if (transform.position != baseLocation)
        {
            StartCoroutine("ReturnToBase");
        }
        else if (transform.position == baseLocation)
        {
            // Add Currency from Pickup
            currencyManagement.AddCurrency(smallPickupWorth);
            
            // Debug.Log("I am at base");

            pickedUp = false;

            if (target == false)
            {
                smallPickup = GameObject.FindWithTag("Small Pickup");
                targetLocation = smallPickup.transform.position;
                target = true;
            }

            if (smallPickup != null && pickedUp == false)
            {
                // Debug.Log("Going for new Pickup");
                targetLocation = smallPickup.transform.position;
                StartCoroutine("MoveTowardsPickup");
            }


        }

    }
}

Use:

GameObject.FindGameObjectsWithTag("Tag");

this will return an array of objects with the specified tag, then just use Random.Range to select one from this array.

private GameObject smallPickup;
private GameObject[] allPickups;

allPickups = GameObject.FindGameObjectsWithTag("Small Pickup");

smallPickup = allPickups[Random.Range(0, allPickups.Length - 1)];

@Fingerlights