- Home /
Instantiate a prefab that has rigidbody cause infinite clones
When i try to instantiate a prefab without rigidbody component, everything works well. But with same script and in same conditions i cant use instantiate for prefab with rigidbody. It just keep creating infinite clones of prefab.
void DropItems()
{
for (int i = 0; i < 3; i++) {
Instantiate(dropItem, dropSpawnPoints[i].position, Quaternion.identity);
}
in Update()
if (health <= 0)
{
DropItems();
transform.GetChild(0).gameObject.SetActive(false);
}
Shortly; i have a parent empty gameobject with this script and there is a tree as child of it. When i break the tree in game it drops 3 wood pieces (prefab without rigidbody). But when i add rigidbody to same wood piece prefab, its getting crazy and throws infinite clones. How can i make it work together with rigidbody component ?
Answer by logicandchaos · Feb 23, 2021 at 02:26 PM
You tell it to instantiate if health is less than or equal to 0, as long as health is less than 0 it will continue you to instantiate. You need to set other restrictions, I see you set transform.GetChild(0).gameObject to false after drop items, so maybe add that to the check?
if (health <= 0 && transform.GetChild(0).gameObject.activeSelf)
Thank you very much for help. After edit the condition as you said , its fixed!. But i didn't understand why my code worked with the prefab without rigidbody but fail with rigidbody.