I’m trying to find a way to instantiate an object and depending on where it spawns it will have a certain location to go to. So lets say it gets instantiated at spawn point1 then I want it to travel toward end point 1. Same with spawn point2 needs to send the object toward end point 2. I’m not sure if I can give it a variable as it’s being spawned to let it know where to travel.
Well if there’s some sort of script on the object then I don’t see why not.
GameObject someObject = Instantiate( somePrefab, spawnPoint1.position, Quaternion.identity );
someObject.GetComponent<someScript>().setTarget( endPoint1 );
After that all you have to do is have the that script do something with that target.
GameObject target;
void SetTarget( GameObject targetObject ){
target = targetObject;
transform.LookAt( target ); //Or whatever you want to do with rotations
}
void Update(){
//Move accordingly
}
Make your object that gets spawned and moves-torwards-a-point a prefab. Then Instantiate it using: Instantiate (object, Position as Vector, rotation); Also when using Instantiate, the prefab will contain all the info, so make a script and attach that move-torwards-the-point script. Then make it a prefab.
1.) Make a object.
2.) Make a script.
3.) Add the script to the object.
4.) Make a empty prefab.
5.) Click on the prefab.
6.) Drag and drop the object on the prefab's Inspector window.
7.) Name it.
8.) Instantiate it by: Instantiate (Object, Position as Vector, Rotation);
9.) Done, it will start doing whatever.