Hi All,
I am new to unity, in my project I want to add the physics to the objects at run-time and remove. I know the method for adding the component, but I don’t know how to remove the component. Please suggest the way to do this.
Use Destroy() for removing components.
–Eric
Thank You
// Kills the game object
Destroy (gameObject);
// Removes this script instance from the game object
Destroy (this);
// Removes the rigidbody from the game object
Destroy (rigidbody);
// Kills the game object in 5 seconds after loading the object
Destroy (gameObject, 5);
// When the user presses Ctrl or Left Mouse Button, it will remove the script
// named myScript from the game object
function Update () {
if (Input.GetButton (“Fire1”) GetComponent (myScript ))
Destroy (GetComponent (myScript ));
}
In editor mode use DestroyImmediate();
Problem though is when you wish to destroy a component in order to recycle the object and then when you re recycle the object in a different time you wish to add that deleted component again ( at runtime).Destroy doesn’t remove the entry or instanceId if you will of that component on the collection that stores references to all of the gameobject’s components.On the editor that works well because the RemoveComponent of the Editor is C++ code whereas runtime is C# scripting and apparently scripts don’t have access to that collection.
It’s a C# call to a C++ engine function either way.
If you want to “recycle” stuff as you mentioned you shouldn’t be using Destroy() at all. The component(s) to be recycled need to be on their own GameObject (because the Transform is how its relationship with the scene is managed), and instead of destroying you should be disabling and re-enabling it, and probably changing what it’s parented to.
for removing component this works
Destroy (rigidbody);
Thanks man it worked
Then how to add Components in runtime
With AddComponent
Hi,
I have an issue with reloading a small ball,when i hit on the blue line ball gets detached but i need to get small ball reloaded with a small delay so that i should get a real feel of ball is detaching and new ball comes in.
Thanks in advance
Thanks.
Cool, thanks.
i actually have an issue where im not allowed to disable certain components like the checkbox is literally missing