How move NPC to detected child gameobject?

Hi all,

Still learning Unity.

I have an NPC randomly walking around a navmeshed map. There are multiple “food” objects (tagged as “food”) laying around and they are all childs of a parent GameObject called “FoodResource”. Once the NPC detects a food object with Physics.CheckSphere(), I want the NPC to move towards that specific food object to collect it.

The problem is that I don’t know how to get that specific food object’s position since Physics.CheckSphere() doesn’t return that information. I could use a Raycast with RaycastHit but then again, I don’t have the direction needed for a Raycast. Any suggestions on how I might go about doing this? Thank you.

Think I figured it out.

You can use Physics.OverlapSphere(npc.transform, SphereRadius, yourLayerMask) to detect all colliders that entered a sphere space without having to create a sphere collider component for the NPC. Since you used the yourLayerMask option, only colliders on your specified layer will be returned in a list/array. You can search through that list/array and pull out the collider you want and get its transform. Example code below:

public class NPC : MonoBehaviour
    // My NPC NavMeshAgent
    public NavMeshAgent agent;

    // Transform of the resource the NPC will encounter
    private Transform resource;

    // Array of colliders that NPC sphere-check will detect
    private Collider[] nearbys;

    // Collider object NPC is specifically looking for
    private Collider resourceCollider;

    // Layer masks for ground and resource
    public LayerMask whatIsGround, whatIsResource;

    // Variables involved with states
    public Vector3 walkPoint;
    bool walkPointSet;
    public float walkPointRange = 5;
    public float sightRange;
    public bool resourceInSightRange;

    private void Awake()
        // Get the NavMeshAgent component of your NPC
        agent = GetComponent<NavMeshAgent>();


    // Start is called before the first frame update
    void Start()


    // Update is called once per frame
    void Update()
        // Check if resource is in sight range
        resourceInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsResource);

        // States available to NPC: Wandering or MoveToResource

        // Resource is not in sight
        if (!resourceInSightRange) 
            // Debug.Log("Wandering");

        // Resource is in sight
        if (resourceInSightRange) 
            // Check to see what colliders entered the overlap-sphere - should only be resource colliders since I specified whatIsResource layermask
            nearbys = Physics.OverlapSphere(transform.position, sightRange, whatIsResource);
            /* Since whatIsResource is specified as the food layer, I know there should only be one food object in the array
            because I set there to be very sparse amount of food on the map. Otherwise, you might have to loop through
            the array to find the specific collider tag you are looking for. */
            resourceCollider = nearbys[0];
            resource = resourceCollider.transform;