Objects Instantiating at wrong position

When I call:

function fireRocket(){

	var rocketclone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation);
	rocketclone.velocity = transform.TransformDirection (Vector3.forward * 300);
	Destroy(rocketclone.gameObject,10);

}

It instantiates the rocket at position (0,0,0) with rotation (0,0,0), however it returns the position as that of the player (at whose position it is supposed to instantiate) when Debug.Log(rocketclone.position) is called.

Help!

FYI:

The script is attached to the player.

function fireRocket() is called from Update thus:

if (Input.GetButtonDown("Fire2")){

	//shoot rocket;
	fireRocket();

}

Entire Script:

//Inspector Variables

var maxSpeed	: float = 80.0;
var minSpeed	: float = 40.0;
var armor		: float = 100.0;
var health		: float = 100.0;
var curSpeed	: float = 50.0;
var turnSpeed	: float = 10.0;
var planecam	: GameObject;
var bullet		: Rigidbody;
var fireSpeed	: float = 0.05;
var rocket		: Rigidbody;
var ammoType	: String;
var rocketAmmo	: int = 8;
var controlNum	: int = 360;
var damage		: float;

//Private Variables
private var turn : float;

function Start(){

	InvokeRepeating("fireGuns",0.0,fireSpeed);

}

function Update(){

	turn = Input.GetAxis("Horizontal")*turnSpeed;
	
	curSpeed = curSpeed + (Input.GetAxis("Speed")*0.2);	
	
	if(curSpeed<minSpeed){
	
		curSpeed=minSpeed;
	
	}
	
	if(curSpeed>maxSpeed){
	
		curSpeed=maxSpeed;
	
	}

	if (Input.GetButtonDown("Roll")){

		//execute aileron roll function;
		controlNum = 0;

	}

	if (Input.GetButtonDown("Fire2")){

		//shoot rocket;
		fireRocket();

	}

	if (Input.GetButtonDown("Special")){

		//execute special function;

	}

	movePlane();
	rollFunc();
	
	if(damage!=0){
	
		calcDmg();
	
	}

}

function movePlane(){

	if(controlNum>=360){

		transform.Rotate(0,turn,0);
		
	}
	transform.Translate((Vector3(0,0,1)*curSpeed)/2,Space.Self);
	
	planecam.transform.position.x = transform.position.x;
	planecam.transform.position.z = transform.position.z;
}

function fireGuns(){

if (Input.GetButton("Fire1")){

		var localOffset=Vector3(20,0,0);
		var worldOffset1 = transform.rotation * localOffset;
		var worldOffset2 = transform.rotation * -localOffset;
		var cannon1 = transform.position + worldOffset1;
		var cannon2 = transform.position + worldOffset2;

		var clone : Rigidbody = Instantiate(bullet,cannon1,transform.rotation);
		var clone2 : Rigidbody = Instantiate(bullet,cannon2,transform.rotation);
		clone.velocity = transform.TransformDirection (Vector3.forward * (400 + (curSpeed*curSpeed)));
		clone2.velocity = transform.TransformDirection (Vector3.forward * (400 + (curSpeed*curSpeed)));
		
		Physics.IgnoreCollision(clone.collider, collider);
		Physics.IgnoreCollision(clone2.collider, collider);
		
		Destroy(clone.gameObject,5);
		Destroy(clone2.gameObject,5);
		
		}
}

function fireRocket(){

	if(rocketAmmo>0){
		var rocketclone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation);
		rocketclone.velocity = transform.forward * 200;
		var rscript : rocketScript = rocketclone.GetComponent("rocketScript");
		rscript.type = ammoType;
		Physics.IgnoreCollision(rocketclone.collider, collider);
		rocketAmmo = rocketAmmo - 1;
	}
}

function rollFunc(){

	if(controlNum<=360){
	
		collider.isTrigger = true;
		transform.Rotate(0,0,5,Space.Self);
		controlNum = controlNum + 5;
	
	}
	else{
	
		collider.isTrigger = false;
	
	}

}

function OnCollisionEnter(collision : Collision){

	

}

function calcDmg(){
	
	var adam = damage/armor;
	var hdam = damage - adam;
	
	armor = armor - adam;
	health = health - hdam;
	
	damage = 0;
	
}

Alright… long time later, but…

I have a script instantiating enemies e random positions, but i did something that was making my enemies being instantiated in wrong positions when i pressed play on the game. I found out that deactivating the Animator in the enemy inspector made the instantiation get correct again. Somehow its bugging the instantiation position…

It’s using the transform.position and transform.rotation of whatever object the script is attached to. If that’s not what you want, you need to reference a different transform. (By the way, you can just do transform.forward instead of transform.TransformDirection (Vector3.forward). )

Do you have an extra object in your scene that happens to also be spawning the rocket that shouldn’t be?

I had the exact same symptoms as you and that ended up being my issue.