# Instantiate cloned prefab to local position of an empty object

 0 Hi, I can't figure out how to get the clones of a prefab to instantiate on the position of the empty game object that this script is on. All the objects that spawn come from the world space of 0,0,0. I want them to spawn FROM 0,0,0 of the LOCAL objects spot. (ie. When I move the parent object, the spawning of the cloned prefabs will follow.) Here is my code I'm using. ``````var prefab : Rigidbody; var speed = 5; var numberOfObjects = 20; var radius = 5; function LaunchingProjectile() { for (i = 0; i < numberOfObjects; i++) { var angle = i * Mathf.PI * 2 / numberOfObjects; var pos = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; clone = Instantiate(prefab, pos, Quaternion.identity); clone.velocity = transform.TransformDirection( Vector3 (0, 1, speed)); Destroy (clone.gameObject, 3); } for (i = 0; i < numberOfObjects; i++) { angle = i * Mathf.PI * 2 / numberOfObjects; pos = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; clone = Instantiate(prefab, pos, Quaternion.identity); clone.velocity = transform.TransformDirection( Vector3 (1, 0, speed)); Destroy (clone.gameObject, 3); } } function Update() { if(Input.GetButtonDown("Fire1")) { LaunchingProjectile(); } } `````` =====Old code above. Newer code below========= ``````var prefab : Rigidbody;//instantiated prefab for clone var speed = 15;//speed that clones travel var numberOfObjects = 15;//Used in Mathf calculation var radius = 1;//used in MathF calculation function Update() { if(Input.GetButtonDown("Fire1")) { LaunchingProjectile(); } } function LaunchingProjectile() { for (i = 0; i < numberOfObjects; i++) { var angle = i * Mathf.PI * 2 / numberOfObjects; var position = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius; clone = Instantiate(prefab, position, transform.rotation); clone.velocity = transform.TransformDirection( Vector3 (0, 1, speed)); clone.transform.parent = transform; Destroy (clone.gameObject, 3); } } `````` more ▼ asked Apr 05 '11 at 12:15 AM Musclegunner 5 ● 4 ● 4 ● 8 First off, sorry about the formatting I'm having trouble trying to paste a code block but I'll figure out in due time Apr 05 '11 at 12:17 AM Musclegunner Select code, hit the 'code' button on the toolbar, looks like 1's and 0's Apr 05 '11 at 12:34 AM DaveA add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 clone.transform.parent = transform; would be my guess. Assumes 'this' object the script is on is the parent? ``````clone = Instantiate(prefab, position, transform.rotation); clone.velocity = transform.TransformDirection( Vector3 (0, 1, speed)); clone.transform.parent = transform; clone.transform.localPosition = position; `````` The 'position' in Instantiate will be world coordinates. Setting the 'parent' will adjust those coords to be relative to the parent, and remain same place in the world. So setting localPosition last should force them to be relative to the parent. more ▼ answered Apr 05 '11 at 12:36 AM DaveA 26.5k ● 151 ● 171 ● 256 Ok that seems to work but it has an issue... When I put it above where the clones are instantiated, it doesn't know what 'clone' is. However when I put it under where the clone is instantiated already, they all spawn on the WORLD 0,0,0 still, however when I move the parent object, they follow along the local axis of the parent object. How do I incorporate taht into my code to have them 'spawn' on the local 0,0,0 Apr 05 '11 at 12:57 AM Musclegunner clone.transform.localPosition = Vector3.zero; Apr 05 '11 at 01:28 AM DaveA Actually, in your case, I think you'd set clone.transform.localPosition = pos; Apr 05 '11 at 01:29 AM DaveA Well I'm sorry to say but nothing seems to be working. I believe the line to be the problem child is clone = Instantiate(prefab, pos, Quaternion.identity); which I now have as clone = Instantiate(prefab, pos, transform.rotation); Which is essentially the exact same thing. If I put your code before that line, Unity understandably freaks out because 'clone' hasn't been created yet. However if I put the code after this line, it has no affect at all because the 'clones' were created already. It's driving me nuts O.o It's probably a simple fix but I'm thinking too hard Apr 05 '11 at 04:14 AM Musclegunner You have to put them after, they need to have been created. Setting the position after creation should work. Apr 05 '11 at 04:23 AM DaveA add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x3455
x3327
x884
x133
x108

asked: Apr 05 '11 at 12:15 AM

Seen: 2462 times

Last Updated: Apr 05 '11 at 05:53 AM