- Home /
Recycling Bullets vs Instantiating?!?!?!
So I have heard differing opinions on whether I should recycle bullets, by storing them in an array or vector and reusing them instead of instantiating a copy every time the fire button is pressed... is there really much of a difference, performance-wise?
Cheers.
Are you talking about actual physical bullets that move through the scene? Or are you doing raycasts?
Answer by svenskefan · Sep 11, 2012 at 05:45 AM
Hi! I totally disagree ;)
Depending on the platform of course, but for iOS and Android, I would definitely recommend to keep the instances hanging around instead of using Instantiate. If you Instantiate and Destroy you will get unpredictable hickups when the Garbage collector does it thing.
Argh! I thought this might happen! The game I'm working on is for $$anonymous$$ac/PC but I may POSSIBLY do a conversion for tablets...
There's a huge difference on handheld devices where operations, such as this, tamper with the available memory. You should definitely reuse all objects that is used repeatedly throughout a scene. I would say that goes for desktops too, especially for projects run inside a browser.
It depends on the layout, a good rule of thumb is if your objects are appearing and disappearing quickly then it's of great use to have batched and cached them. Then I usually make my own "Update"-loop for these items so you don't have to activate them.
private var myRenderer : renderer;
function Start () {myRenderer = renderer;}
function OnBecameVisible () {
while (myRenderer.isVisible) {
//Logic
yield;
}
}
Caching is king. :-)
Answer by Devon Hall · Sep 11, 2012 at 04:35 AM
When it comes to Instantiation, it doesn't take much to perform.
To keep the instances in the scene in an Array would be a lot, I mean a lot less cost effective in performance. Even if you're creating quite a bit of bullets its really worth the 3 lines to instantiate then destroy.
Stick to Instantiate and Destroy. I find performance much better than theoretically storing each instance of an object for a period of time in which users might not need them.
Yeah, it's not like creating whole new objects, is it? Thanks for your help :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613080906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Good Setup for Predefined Array Data? 0 Answers
going through each item of a multi-dimensional array 1 Answer
What is the best way to clear an array of Components? 2 Answers
hi!does any know how to make an endless track 1 Answer
Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers