- Home /
Instantiate at Child Object
Parent is the 'Player' that holds the script that fires. I would like to instantiate the 'laserBeam' from an Empty Child Object (named LeftGunFire) directly in front of the gun barrel. I'm not sure where I'm going wrong. Below is the instantiating part I believe is in question.
var LeftGunFire : GameObject; //Where I want it to instantiate from.
var clone : Rigidbody;
clone = Instantiate(projectile, LeftGunFire.transform.position, transform.rotation);
clone.velocity = transform.TransformDirection (Vector3.forward * 1);
Define "going wrong". Also, there's nothing called "Parent" in that. The parent-child relationship seems unrelated to the question you're asking, so you probably should edit the title.
Answer by Bytebait · Jan 10, 2012 at 04:29 PM
I've added 'child object' back to the tag as that is where I found an API hint to the solution.
For those who have the same issue as I did, here is how I solved instantiating to a child object from the parent.
var projectile : Rigidbody;
function Update () {
if (Input.GetButton ("Fire1")){
var clone : Rigidbody; aLeftFire = transform.Find("LeftGun/LeftGunFire");// LeftGunFire is an empty game object
//to always avoid collision issues with projectiles
clone = Instantiate(projectile, aLeftFire.position, aLeftFire.rotation); clone.velocity = aLeftFire.TransformDirection (Vector3.forward * 10); } }
Answer by Bytebait · Jan 10, 2012 at 03:16 PM
Thank you for the heads up Jessy. "NullReferenceException: Object reference not set to an instance of an object" is the full error displayed.
I have a prefab with several objects inside it. The top hierarchy of the prefab (if my terminology is correct) is called the Parent. From the Parent, I'm trying to instantiate an object at the location of the child object.
This will instantiate the object from the Object that calls the script from itself.
var clone : Rigidbody; clone = Instantiate(projectile, transform.position, transform.rotation); clone.velocity = transform.TransformDirection (Vector3.forward * 1);
But how would I instantiate it at a Child Object within itself? Also, if my terminology is wrong, please correct me.
Prefab
-Player (Script Run Here)
-Left Wing
-Left Gun
-LeftGunFire (Location I wish to 'shoot' projectile from)
Your answer
![](https://koobas.hobune.stream/wayback/20220613062820im_/https://answers.unity.com/themes/thub/images/avi.jpg)