- Home /
Instantiating an object and Re-parenting kills Performance
I am trying to allow the player to click on a target spaceship and create a GUI texture object that moves with the ship to show that it is targeted. I do a Raycast and if it finds a ship under the mouse click, it instantiates a new prefab game object. I set this new object's transform.parent to the ship found from the raycast.
Doing this just a few times kills the frame rate pretty significantly. The framerate doesn't recover after the instantiation either. This is surprising because the prefab only has a transform and a script where everything is commented out. Is this behavior expected? If so, are there any work-arounds?
Okay so I found a work-around. I discovered that adding several children to an object with a rigid body causes performance to drop. However, if I create a dummy object that is child of the object with the rigid body, and then add a whole bunch of objects to that child, then everything runs smoothly.
It's not clear to me why the rigid body makes such a huge difference because the children never have any colliders or rigid-bodies themselves. Perhaps someone with more experience knows?