x


transform array in javascript

hi there, this is tom again. after finishing my last project with some helpfull hints from the folks out here, I started a new project with new problems. I build a little plaza and a seeker drone should move between some points. for that I use the "SeekSteer" script from the unity Wiki pages. it works very fine when I use a drone in normal way. the script defines the destination points in an array in form of JAVASCRIPT "var waypoints : Transform[]". when I change the seeker object to a prefab, then it looses all the preset waypoints and i cant set them. do anybody has experience with that script or can tell me how to define the waypopints within the script in an form like:

waypoint[0] = Transform p1, waypoint[1] = Transform p2, waypoint[2] = Transform p3,

... thx a lot from berlin tom

more ▼

asked Feb 02 '10 at 04:32 PM

Tom de Jank gravatar image

Tom de Jank
273 7 7 12

What are "p1", "p2" and "p3"? are they the names of GameObjects?

Feb 02 '10 at 04:34 PM duck ♦♦

hi duck. yes p1, p2 ... are the names of the GameObjects. this are emty objects

Feb 02 '10 at 04:57 PM Tom de Jank

one more time lots of thanks duck. nest time we have a beer...

Feb 02 '10 at 05:16 PM Tom de Jank

no problem :) if it solved your question, please click the 'accept' button.

Feb 02 '10 at 06:36 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

To build a list of those object's transforms at runtime, you could use something like this (if you know the number of waypoints in advance):

// in c#
Transform[] waypoints = new Transform[10];

void Start()
{
    for (int n=0; n < waypoints.Length; ++n)
    {
        waypoints[n] = GameObject.Find("p"+(n+1)).transform;
    }
}


// or in Javascript
var waypoints : Transform[] = new Transform[10];

function Start() {
    for (var n=0; n < waypoints.Length; ++n) {
        waypoints[n] = GameObject.Find("p"+(n+1)).transform;
    }
}

Notes:

  • Set the number of waypoints in the line which declares the array (10, in the example above)
  • The gameObject lookup is case sensitive, so it will find objects called "p1" to "p10", but not "P1".
  • I didn't add code which checks the existence of the objects, so if one of the gameobjects does not exist or is mis-named, you'll get a null reference error.

If you have numerous scenes or situtaions where the number of waypoints might vary, you could modify this code to dynamically discover the number of waypoint objects first, before building the array. That way you wouldn't have to have a hard-coded number in your script which determines the number of waypoints.

more ▼

answered Feb 02 '10 at 05:12 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

(comments are locked)
10|3000 characters needed characters left

Plae new java script send me my Email iD

more ▼

answered May 04 '10 at 05:30 AM

irshad khan gravatar image

irshad khan
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5051
x1273
x351
x4

asked: Feb 02 '10 at 04:32 PM

Seen: 6135 times

Last Updated: Feb 17 '10 at 12:21 PM