Sadly I’ve wasted quite a few hours trying to crack this using an Array, but I keep failing.
With an Array I’ve been able to select a random GameObject and populate an object field using…
WayPointArray = (GameObject.FindGameObjectsWithTag("WayPoint"));
with
other.GetComponent<NPC_AI>().WayPoint = NPCWayPoint;
To find a way to get my NPCs to walk to random waypoints.
Naturally rather than having my NPCs active across a large map, I’m trying to keep them local to the player. Having a map of Spawn and WayPoint objects which direct the NPCs where to walk to next within an area.
On ‘Start’ the WayPoint script finds GameObjects tagged with ‘WayPoint’, and when it’s collider is triggered by an NPC, the script delivers a position for the NPC to go to. …using the code exampled above.
Only the problem is, my coding isn’t great and I can’t get the individual Arrays to only find tagged WayPoints within a small distance from each other, building a mesh of points that help the NPCs go A>B rather than A>Z across the map. My limitations in code means each WayPoint finds every WayPoint Tag across the map. Not ideal.
So I’ve now turned to Lists instead (for the first time), finding that On Start I can now get each WayPoint to find each other within a distance using a collider. (Couldn’t with an Array)
public List<GameObject> WayPoints;
void OnTriggerStay(Collider other)
{
if (other.tag == "WayPoint")
{
if (!WayPoints.Contains(other.gameObject))
{
WayPoints.Add(other.gameObject);
}
}
}
With that working as hoped, I now have the opposite problem where I can’t get my other NPC_AI script to select a random WayPoint GameObject from the above.
Is there anyone that can help? The only way I could easily get my WayPoints to find each other close by, was to switch to lists instead.
As I mentioned, I’m not brilliant at coding generally and have managed with Arrays till now, but Lists so far have been alien to me and not as similar as I was hoping.
Any help I’d be really grateful. Thank you!!