How to Instantiate Properly Your GameObject using Addressable Assets to Free your Memory

Prasetio Nugroho
3 min readAug 4, 2023

In Unity, instantiating GameObjects is a fundamental and powerful concept in game development. When we instantiate a GameObject, we create a new instance of it during runtime, allowing us to dynamically generate and manage objects in the scene. This process is commonly used for creating characters, enemies, power-ups, or any other interactive elements. Properly handling instantiation is essential for memory optimization and overall performance, as it involves managing resources and ensuring a smooth gaming experience for players.

Instantiate function in Unity’s scripting API

Using the Instantiate function in Unity’s scripting API, developers can duplicate Prefabs (pre-configured GameObject templates) and position them within the scene based on specific requirements. Like this example, when I press Space on the keyboard it will instantiate 3d environtment model.

press space to instantiate prefab

Let’s try to build and check the memory consuming. When we start the build and open task manager and check memory process that it’s consume around 96 MB in average at start before we press space, and after we press space to instantiate the object, it consumes araund 120 MB. There is no significant difference.

memory before instantiation
memory after instantiation

Is it still possible to free more memory usage?. Yes, using addressable assets.

Instantiate function using Addressable Assets

First thing, we should setup the prefab that we should instantiate into addressable group configuration, I will skip this step, you can check step for setup in my previous article.

prefab configuration for addressable assets

In spawner script, I use InstantiateAsync method that provide by addressable package to made Instantiation.

new spawner script with addressable assets

Let’s check the memory consume. Before build the game, build the bundle first in Addressable Group Configuration, Build -> New Build -> Default Build Script.

Build bundle in Addressable Group Configuration

After build the bundle, let’s play the game and check memory consume before and after Instatiation in task manager.

memory before instantiation
memory after instantiation

There is so much difference, before instantiation it consumes around 56 MB, after instantiation it consumes around 125 MB. It’s clearly that we can free some memory from start and when we don’t need it.

Conclusion

I think when our game is bigger and need to load big assets, you can try this approach to reduce some memory usage. And because this method is Async you can use loader for better experience when player need to wait your assets created.

My Reference

Code Monkey — How to use Addressables FASTER Loading FREE Memory SMALL Download

Memory Management Addressable Assets

--

--

Prasetio Nugroho

Interested in new tech, gaming, programming and another knowledge or possibilites that can make me better. Cheers!!