How can I get my NPC AI to locate all objects within X distance, identify each object and then be able to Target a specific object? An example being: The AI is in an area, and there are 4 objects within it's range. Those objects are 2 tree, a rock, and a pig. The AI needs wood, and then will need some rock. How do I get the AI to be able to identify the 2 tree, the rock, and the pig, and then go to a tree, and then go to the rock?
2 Answers
To search for certain type objects:
var foundObjects = FindObjectsOfType<TextMesh>();
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
To locate them (note that foundObjects is object[], you need to loop over them):
foundObject.transform.position;
Needs can be stored in priority queue. It is basically self ordering data collection, if you solve the highest priority task of ex: finding 2 wood, it will get you the next highest priority task to do.

- 19,694
- 14
- 55
- 103
This question is broad, There are so many aspects to going about this. I suggest attempting one aspect / researching it. Once you come to a stump in code that you cant understand, come ask. If I had to suggest a starting point, I would look up how to code the distance from one object to another. Then look up how to do that for multiple objects in a scene. Then look up how to identify those, whether it be by Tag or GameObject, etc..
Start from there, I wish you the best of luck on your journey!

- 95
- 1
- 8