- Home /
How do I add a rigidbody in script?
Hi,
Is it possible to add a rigidbody to a MonoBehaviour object in code? If so, how would I do this?
I'm hoping to have no rigidbody on the object and then add one to it on a button press.
Cheers :)
Answer by Digitalos · Jun 13, 2010 at 05:21 PM
You need a reference to your gameObject, and then you use AddComponent: http://unity3d.com/support/documentation/ScriptReference/GameObject.AddComponent.html
C# Code:
GameObject myGameObject = new GameObject("Test Object"); // Make a new GO.
Rigidbody gameObjectsRigidBody = myGameObject.AddComponent<Rigidbody>(); // Add the rigidbody.
gameObjectsRigidBody.mass = 5; // Set the GO's mass to 5 via the Rigidbody.
Can you kindly tell as how to modify OnCollisionEnter function for gameObjectsRigidBody created above or as how to add another script for the rigidbody object just created in such manner.
Your answer
Follow this Question
Related Questions
How to use rigidbody for characters 0 Answers
Tank-Based Creation of Tank-Based Objects 0 Answers
OnTriggerEnter without RigidBody 3 Answers
Network rigidbody synchronize 0 Answers
Velocity always reads zero 0 Answers