The first time I Instantiate prefabs in my game (particularly ones that use large bitmap textures) there is a noticeable lag / delay before they appear, subsequent times after this they then appear instantly. Is the best way of dealing with this to Instantiate then Destroy all the problematic assets before first gameplay?
Answer by duck
Nov 17, 2009 at 11:33 AM
A useful technique for iPhone is to instantiate things that you'll need for your gameplay during your menu screens, so that by the time the user clicks 'play', everything has already been preloaded.
Also, wherever possible, re-use a pool of instantiated objects rather than destroying and then re-creating objects each time new ones are required.
Also you should spread out your instantiation over a number of frames, so that there isn't a large hiccup caused by a number of items being instantiated all during the same update.
You might find you need to write some kind of instantiaton manager, which handles instantiating a queue of items over a number of frames.
Answer by equalsequals
Nov 26, 2009 at 09:43 PM
To add to Duck's comment,
With iPhone the garbage collection is a bit tricky. Even if you destroy the only instance of an object, garbage collection can neglect to clean it up. In a previous project we found that several "destroyed" Game Objects were still in memory after dumping old scenes and loading new ones.
We found the only way to truly destroy dynamically instantiated objects was to do the following:
myObj = new GameObject();
This will let GC know that you are using that variable for something else now and that it is safe to be collected.
As loginstubbornmoose's question went unnoticed, I'd like to ask the same... because your example would instantiate a new GameObject which should be avoided if possible, no?
Answer by stubbornmoose
Nov 27, 2009 at 03:31 PM
Will it help to GC if we do this?
myObj = null;
Answer by equalsequals
Jan 08, 2010 at 08:12 PM
To answer SisterKy:
Yes, you do want to avoid this in most cases. This is just the solution I found for a very specific case.
To explain, when developing for iPhone my team found that memory remained allocated after GC passes even though the object had been destroyed. As an important note I should mention that the GameObject in question was dynamically instantiated from Resources.Load(), not manually instantiated via the Editor.
We tried setting all references to the object to null, but this made no difference to the memory as per Instruments' readout. What we discovered was that if we immediately instantiated a blank object in place of the destroyed object, GC did pick it up and memory was freed.
Does that clear it up at all?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users post good questions and use the site effectively we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
The name 'Joystick' does not denote a valid type ('not found')
How to implement an ammo counter using textured quads? [Scripting doubts]
Swiping images across the screen - iPhone
What is the expected behaviour of Dynamic Batching?
Whats the best way to setup a screen like this...