//Called when spawning an agent prefab without assigning any waypoints to it. //The method looks for the closest waypoint assigner and read the waypoints in it //into the agent itself. private void LookForWayPointAssigner() { GameObject aiWayPointHolders = GameObject.Find("AIWayPointHolders"); Component[] assigners = aiWayPointHolders.GetComponentsInChildren(typeof(WayPointHolder)); if (assigners.Length != 0) { float distanceToHolder = Mathf.Infinity; WayPointHolder closestWayPointHolder = null; foreach (WayPointHolder wayPointHolder in assigners) { if (Vector3.Distance(transform.position, wayPointHolder.transform.position) < distanceToHolder) { distanceToHolder = Vector3.Distance(transform.position, wayPointHolder.transform.position); closestWayPointHolder = wayPointHolder; } } wayPoints = closestWayPointHolder.wayPoints; patrolPoints = closestWayPointHolder.patrolPoints; } }
void Start() { waypointHolder = GameObject.Find("WaypointHolder").GetComponent <WayPointHolder>(); }