# Creating A Basic Path For Enemies

**URL:** https://discussions.unity.com/t/creating-a-basic-path-for-enemies/67001
**Category:** Questions & Answers
**Created:** [March 26, 2013, 7:27pm UTC](https://discussions.unity.com/t/creating-a-basic-path-for-enemies/67001 "2013-03-26T19:27:18Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ProjectCryken](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/projectcryken/32/372619_2.png) [@ProjectCryken](https://discussions.unity.com/u/ProjectCryken)
#### Post date: [March 26, 2013, 7:27pm UTC](https://discussions.unity.com/t/creating-a-basic-path-for-enemies/67001/1 "2013-03-26T19:27:18Z")

</div>

I am unsure of how to achieve this in UnityScript:

1.Save all GameObjects with tag ‘way’ to an array

2.Sort the array alphabetically

3.Each ‘way’ object contains 4 GameObjects saved to variables named : ‘path1’ ‘path2’ ‘path3’ ‘path4’ in a script called ‘WaypointScript’. Based on a number I supply it (1-4) it must go through the Array and save each path object from each way object in another array.

**For example: Should the number supplied be 3, it would run through each way object and add the path3 variable to a new array. Resulting (If there were 4 way objects) with an array containing way1’s path3, way2’s path 3, way3’s path3 and way4’s path3.**

Thanks a lot, I hope to learn from any answers I receive (Im not just being lazy)

---

<div class="post-metadata">

### Author: ![whydoidoit](https://avatars.discourse-cdn.com/v4/letter/w/90db22/32.png) [@whydoidoit](https://discussions.unity.com/u/whydoidoit)
#### Post date: [March 26, 2013, 8:01pm UTC](https://discussions.unity.com/t/creating-a-basic-path-for-enemies/67001/2 "2013-03-26T20:01:12Z")

</div>

I’d suggest you put your path1, path2 etc into an array. Then you can get your new array like this:

```
  import System.Linq;

  ...

  var taggedObjects = GameObject.FindGameObjectsWithTag("way");
  var pathNumber = 3;
  var path3 = taggedObjects
        .OrderBy(function(g) g.name)
        .Select(function(g) g.GetComponent(WayPointScript).path[pathNumber-1])
        .ToArray();

```

Using your existing method:

```
  var taggedObjects = GameObject.FindGameObjectsWithTag("way");
  var pathNumber = 3;
  var path3 = taggedObjects.OrderBy(function(g) g.name).Select(function(g) {
         var wayPointScript = g.GetComponent(WayPointScript);
         if(pathNumber == 1)
              return wayPointScript.path1;
         if(pathNumber == 2)
              return wayPointScript.path2;
         if(pathNumber == 3)
              return wayPointScript.path3;
         if(pathNumber == 4)
              return wayPointScript.path4;
  }).ToArray();

```
