Assiging waypoints through a script.

Hi Unity users!

I have made a simple waypoint script,but in my scene I have like 60-70 waypoints.
How can I make a script so my waypoints are attached automatically to the script,instead of putting them into their slots manually?

Thanks in advance!

-Robert-

If they are tagged, I would use:

var waypoints  = GameObject.FindGameObjectsWithTag ("waypoint");

That would put them all in an array.

However, if you want them to be in the array in any meaningful order that isn’t mathematically calculatable, you’ll probably have to do it manually.

or you can name each object properly, and sort them by name if needed

:frowning:

So…I have to do it manually.

nah, when using

var waypoints  = GameObject.FindGameObjectsWithTag ("waypoint");

you get an array of gameobjects
all you need is to sort this array in you script

What Tutanhomon said.

Once they’re in an array, from there, its figuring out the order you want the waypoints to be in, assuming the order is actually something that can be mathematically computed, like distance from the AI or something.

If the order you want the waypoints is based on something arbitrary like personal preference or how you laid out your level by hand, then yeah, you have to do it by hand. Unity is pretty damn powerful, but unfortunately it can’t make human decisions, read your mind, or anticipate errors-by-design :wink: