Data transfer between scenes in Unity - the use of multi-value in the development of simple games

This article will be useful for novice game developers. In it, I will talk about my own experience in implementing multiscended interaction and the problems that I have encountered.

Let's talk about static classes for data storage, various ways of loading scenes with the Unity engine, and also let's take a look at how Admob connects to the project. Information provided by official documentation and friendly developer community.


My game consists of two scenes - the main menu, which can be seen immediately upon loading and, directly, a game scene with mechanics, into which, depending on the option selected, the object prefab is loaded. I couldn’t combine them into a single scene, since several rather complex objects are tied to the menu, and it’s more convenient to separate the entities.

Previously, I would simply use a certain controller object to store data, but with the unloading of the scene, it ceases to exist.

Data transfer (static class)

It turned out that Unity perfectly knows how to work with code, even if it peacefully lies in a file in the scripts folder and is not attached by a component to an object on the scene (it was not obvious to a beginner). For example, such a file could be a static class of the following type:

using UnityEngine;
{privatestatic GameObject prefabName;
    publicstatic GameObject Prefab
            return prefabName;
            prefabName = value;

Thus, I managed to keep the user's choice even after unloading the scene and loading a new one. Initially, everything worked exactly with hard scene switching.

Using this mechanism, you can also transfer settings from the menu to other scenes, for example, the localization language, sound and music settings, and much more.


Everything was fine with me, and so, until the task was received to connect to the Admob project (advertising), so that the video was shown right at the beginning of the game scene. As it turned out, there are subtleties here: the request for a movie takes substantial time, and it simply does not have time to come when switching scenes. We didn’t want to sculpt additional delays in the project, especially since we have a lot of time until the player “sticks” in the menu. Then I learned that there is no need to switch scenes "hard", because there is a wonderful option of additive loading (without unloading the previous scene).

I load the game scene with the menu controller (the scene with the menu and the object of the advertisement remains loaded too):


Upon completion of the level, I unload the game scene with the game controller (so that it does not hang in memory):


With the use of such a scheme, advertising is loaded immediately at the start of the application, and you can trigger the display of the commercial at any desired moment. Similarly, you can deal with any objects you need.


Unfortunately, even with additive loading of scenes, it will not be possible to delve into the objects of one scene from another. References to objects will have to be passed through a certain “mediator” (in my case, that static class was used).

Be careful when instantiating prefabs, if several scenes are active - for me they all decided to push into the wrong scene (about this another time).

Also popular now: