How can I collect the positions of my waypoint objects which mark out a path or route?

Hi guys, im quite new to unity so this is probably something quite simple. Im attempting to use this code here:

to get my object to follow the curve - all is working fine.

However, instead of getting the 'waypoint' position info via the inspector this way:

var points : Vector3[];

( where i have to type the x,y,z into the inspector )

Is there any way of getting the x,y,z info of the waypoint objects i have added to the scene and then having that populate the Vector3 array in the code??

preferably using this way:

var waypoints : Transform[];

I think its all the builtin array stuff thats throwing me.



p.s. should i delete this question from the forum as i have repeated it there ?

Yes I do this often (for example to collect the waypoints which mark out a racetrack).

If your waypoints are numbered, you can search for them by name like this:

var markersList = new ArrayList();
var markerNum = 0; 
var searching = true;

while (searching)
    var markerName = "routemarker" + markerNum;
    var markerObject = GameObject.Find(markerName);

    if (markerObject != null)
        searching = false;

Debug.Log("Found " + markersList.Count + " route markers");

If you need to, you can then convert the ArrayList to a built-in array like this:

var markers = markersList.ToArray(typeof(Vector3));

If your route markers are numbered with zero padding (i.e. 001, 002 etc), you can find their names like this:

var markerName = "routemarker" + markerNum.ToString().PadLeft(2,'0');

I think it's best to numerically name your markers, however if your markers aren't numerically named, you could get them all by assigning a custom tag to them all, and then using FindGameObjectsWithTag, however I'm not 100% sure that they will always be collected in the correct order.

If you use the array of Transforms as you suggest, you can conveniently drag nodes into the array, and copy their positions with the following snippet:

var waypoints : Transform[]; // for inspector
private var waypointVectors : Vector3[]; //for use in script

function Start () //assumes nodes will not change wile the game is running
    waypointVectors = new Array(waypoints.length);

    for(i = 0; i < waypoints.length; i++)
        waypointVectors _= waypoints*.position;*_