When I use Object.Instantiate, it creates a bunch of clones
I use the code:
GameObject fire = (GameObject) GameObject.Instantiate(gameObject, transform.position, transform.rotation);
to create a clone of my bullet, and when I use it for the first time per scene, it works as expected, but on the second time it actually creates 3 clones, and on the third time it creates more clones than I can count.
Is there any solution?
EDIT: apperantly its pattern is: 1 1+2 1+2+4 1+2+4+8...
Answer by rutter · Nov 09, 2015 at 06:22 AM
When you use Instantiate
to create a clone, all of the scripts and components attached to the source object will be duplicated on the resulting clone. In this case, you're cloning gameObject
, which means that the clone will also have a copy of this script that just made the clone.
Depending on how you run your logic, that means that your clones may also start spawning clones. Many beginners first run into this behavior when it creates an infinite loop and crashes their game.
So, generally speaking, you don't want to clone yourself. Clone some other object like a prefab, instead.
Your answer
Follow this Question
Related Questions
Adding a unique element from one list to another list 2 Answers
I need help about Basic Basketball Game 0 Answers
How do I Instantiate an object that is half the size of the parent object? 0 Answers
Random.range game object destroying itself before reaching destroy position.Please help. 1 Answer
Can't Get Prefab Clone To Follow Mouse 2 Answers