- Home /
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
What are "p1", "p2" and "p3"? are they the names of GameObjects?
hi duck. yes p1, p2 ... are the names of the GameObjects. this are emty objects
one more time lots of thanks duck. nest time we have a beer...
no problem :) if it solved your question, please click the 'accept' button.
Answer by duck · Feb 02, 2010 at 05:12 PM
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.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Conceptual Question on Creating Arrays at Runtime 1 Answer
I have never seen a null reference exception like this one! 1 Answer
NullReference when accessing GameObject in array (C#) 1 Answer
I need to know how to rotate a gameobject in all directions. 0 Answers