- Home /
Addforce after Instantiate
I have a rectangle with a script on it.
public Rigidbody rgb;
void Start()
{
}
void OnCollisionEnter (Collision collision)
{
Vector3 contactPoint = transform.InverseTransformPoint (collision.contacts[0].point);
if ((contactPoint.x > -0.2f) && (contactPoint.x < 0.2f))
{
rgb.AddForce (transform.up * 400);
}
else
{
rgb.AddForce (transform.up * 250);
}
}
}
I create spheres using "Instantiate" every 4 seconds ,and they fall on the top of rectangle , and "Addforce" should create the bounce effect. The sphere is a prefab. In the script im using prefab's Rigidbody .
The problem is that , everytime when "Instantiate" creates a sphere, the script on my rectangle does not see the rigidbody of it .
Please help me to figure out , how can i make the script see the rigidbody of the cloned sphere. Thank you in advance !
Answer by jprocha101 · Jul 03, 2015 at 02:22 PM
Try getting the sphere's rigidbody like this then applying the force.
Rigidbody rb = collision.gameObject.GetComponent<Rigidbody>();
rb.AddForce (transform.up * 400);
Answer by meat5000 · Jul 03, 2015 at 05:02 PM
Rigidbody mySphere; //Not Gameobject mySphere. This is the container for your prefab.
~~~~~blah blah~~~~~~
Rigidbody rb = Instantiate(GUBBINGS) as Rigidbody;
rb.AddForce(x,y,z);
I think thats how it goes anyway. Its in the docs.
You may not require "as Rigidbody".
Your answer
Follow this Question
Related Questions
How to gradualy decrease all rigidbody added forces? 0 Answers
Add force to instantiated prefabs (c#) 0 Answers
Rigidbody Player AddForce issue 2 Answers
Torque applied only after force 0 Answers
How to get force applied to object during FixedUpdate 1 Answer