- Home /
assign waypoints on a prefab
Hi,
I am working on a very simple AI just want the enemy to follow a set of way points.
Ie WaypointA,WaypointB,WaypointC
so I created a prefab and want to assigned all the waypoins to it .
If I take a copy of the prefab to the stage and assign the waypoints in the inspector it works.
but the new spawn copys or instants of the prefab that are being spawn are missing the waypoins... I did hit apply on the one I created on the stage.
Here's part of the script:
//array
var waypoint:Transform[];
private var currentWaypoint:int;
var distance;
function patrol () {
if( currentWaypoint< waypoint.length){
var targetwaypoint=waypoint[currentWaypoint];
//var moveDirection:Vector3=target-transform.position;
MoveTowards(targetwaypoint.position);
//animation.CrossFade("walk");
moveSpeed = 5.0;
}
else{
currentWaypoint=0;
}
}
Thank you
Answer by BilboStabbins · Dec 04, 2012 at 10:39 PM
2 methods that I know of;
One way to do this would be to have a script on the prefab that holds a global array/List of the Waypoints which you would have to drag into the slots in the Inspector. When you hit apply, this will remain part of the prefab together with the local positions of the waypoints.
An alternative might be to have an empty object inside the prefab that contains all of the waypoints and have a script search for and copy into an array/List the waypoints upon instantiation of the prefab (Awake method). The waypoints could be search for via their tag, so they could have a 'waypoint' tag, for example.
Hope that helps :)
Thank you for answering.. the first approach did not work--That's the one i was having issue with..even though i hit apply several times.
I was able to solve it by this code(just in case anyone ever needs the solution):
var waypoint:Transform[];
function Start(){
waypoint=new Transform[7]; for(var i=0;i<7;i++){ waypoint[0]=GameObject.Find("waypoint1").transform; waypoint[1]=GameObject.Find("waypoint2").transform; waypoint[2]=GameObject.Find("waypoint3").transform; waypoint[3]=GameObject.Find("waypoint4").transform; waypoint[4]=GameObject.Find("waypoint5").transform; waypoint[5]=GameObject.Find("waypoint6").transform; waypoint[6]=GameObject.Find("waypoint7").transform;
}