- Home /
Instantiate Reference Problem
Hey All!
First post here, so please go easy on me (I'm new to Unity) :-)
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
Answer by daphydthebard · Oct 03, 2011 at 07:37 PM
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)) );
}
}
Your answer
Follow this Question
Related Questions
i attach the prefab, but when i hit play, its not the prefab anymore 0 Answers
I cannot see my cloned prefabs. 0 Answers
Prefab clones behaviour 0 Answers
Help needed with instantiate 0 Answers
finding instantiated prefabs 2 Answers