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);

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);
}
}

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)