Accessing parts of a model from a script

Hello,
I’ve tried searching for this using google and via the community but can’t seem to find any reference to it.

My current situation is this:
I have a model of a spaceship (created using Maya) which also includes 2 place holders called Gun_Left and Gun_Right from which I emit my bullets whenever the “fire” button is pressed. The way the bullets are emitted is via a script which is attached to both Gun_Left and Gun_Right. i.e. 1 script attached to 2 points on the model.

My question:
Is there a way in which I can have a single script which is attached to my root spaceship model (Fighter_2_4) so that I can use just that script to fire using the position and rotation of my gun ports?

My model hierarchy is as follows:
Fighter_2_4
|group2
|group3
|
Gun_Left
|
Gun_Right

My current script looks like this:

// Script to shoot projectile

var Fighter : GameObject;				// The main fighter
var ProjectilePrefab : Rigidbody;		// The bullet prefab to be used
var ProjectileSpeed : int = 40;      // Speed of the bullets
private var resultantspeed : int = 0;


function Update () {
	if (Input.GetButtonDown("Fire1")) {
		// Fire1 button detected
		
		// Get the script component
		var flightcontroller : Fighter_Character = Fighter.GetComponent(Fighter_Character);
		
		resultantspeed = flightcontroller.flightspeed;
		// Get the speed of the projectile and add it to the speed of the fighter
		resultantspeed = resultantspeed + ProjectileSpeed;
		
		// Create new projectile
		var Projectile : Rigidbody = Instantiate(ProjectilePrefab, transform.position, transform.rotation);
		
		// Set the projectiles velocity in the z direction but transfrom from local to world orientation
		Projectile.velocity = transform.TransformDirection(Vector3(0,0,resultantspeed));
		
		// Make sure that the bullet does not collide with the gun muzzle object
		Physics.IgnoreCollision(Projectile.collider, transform.root.collider);
	}
		
}

Move your firing code out of the Update and into its own custom method, then just call that method whenever you want to fire:

on your spaceship:

function Fire()
{
	this.GetComponent("Gun_Left").Fire();
	this.GetComponent("Gun_Right").Fire();
}

On your guns:

function Update()
{
	if (Input.GetButtonDown("Fire1")) 
	{
		// Fire1 button detected
		this.Fire();
	}
}

function Fire()
{
	// Get the script component
	var flightcontroller : Fighter_Character = Fighter.GetComponent(Fighter_Character);
	
	resultantspeed = flightcontroller.flightspeed;

	// Get the speed of the projectile and add it to the speed of the fighter
	resultantspeed = resultantspeed + ProjectileSpeed;
	
	// Create new projectile
	var Projectile : Rigidbody = Instantiate(ProjectilePrefab, transform.position, transform.rotation);
	
	// Set the projectiles velocity in the z direction but transfrom from local to world orientation
	Projectile.velocity = transform.TransformDirection(Vector3(0,0,resultantspeed));
	
	// Make sure that the bullet does not collide with the gun muzzle object
	Physics.IgnoreCollision(Projectile.collider, transform.root.collider);
}

Just whipped this together, adapt it as you need to.

Hi,

Thanks for the info, but this is not what I’m looking for. I would like to completely take the firing script out of my guns and only have it in the script attached to my script.

Ok, do it that way then. What’s the problem?