Instantiate Reference Problem

Hey All!

First post here, so please go easy on me (I’m new to Unity) :slight_smile:

I’m using the following code in a script attached to the main camera to instantiate and apply force to an object ( a ragdoll in this case ) -

var target : GameObject;

function Update()
{
if (Input.GetKeyDown(“space”))
{
var clone : GameObject;
clone = Instantiate(target, Vector3(0,1,-4), Quaternion.identity);

	clone.Find("base").rigidbody.AddForce( target.transform.forward * 20000);
	clone.Find("base").rigidbody.AddForce( target.transform.up      * 10000);	
}	

}

It works fine the first time I press space, but if I press space a second (or third, etc) time, a new ragdoll is created in the scene, but the force get’s applied to the FIRST one, always.

I don’t understand why the force is not being applied to the clone that is being created inside the function.

Any help would be appreciated.

Thanks!

Daphyd

I solved my own issue with the following code:

function CreateClone()
{
var newOne : GameObject;
newOne = Instantiate(target, Vector3(0,1,-4), Quaternion.identity);

	if (newOne != null)
	{	
		newOne.transform.rotation.y = 180;		
		var rb : Rigidbody = newOne.GetComponentInChildren( typeof( Rigidbody ) ) as Rigidbody;			
		rb.AddForce( -newOne.transform.forward * (25000 + Random.RandomRange(1000,75000)) );
		rb.AddForce(  newOne.transform.up      * (15000 + Random.RandomRange(1000,75000)) );
	}	

}