Tips On Organizing Your Unity Scripts In Multiple Scenes


This devlog is for all aspiring Unity developers. For many beginners, Unity coding can be quite daunting to learn since there are so many different Unity specific classes and methods. Once you have learned the basics and created a scene, the next step is building on that scene by making more. The tips and info I'm about to talk about that I've learned while working on Matter of Great Import hopefully will help you manage the scenes in your game, manage your scripts throughout all the scenes, and minimize the work you need to do when creating another scene.


Use Static Variables

Static variables carry over from one scene to the next, which means that any values you don't want to be wiped between scenes (such as an integer representing the current level) can be brought over.  You can also use a static class script, which is an entire script that is carried over from scene to scene and only allows static variables and methods. This can make all your static variables more organized and easier to find. One last thing to be careful about is the fact that any type which inherits from UnityEngine.Object gets destroyed between scenes unless you use the method DontDestroyOnLoad on that variable.

On Matter of Great Import, I decided to create a static script since I like how organized it makes my variables. The variables I stored in it are the current level number, all the objective strings in the game, and which objectives have been completed in each level stored as booleans. I mainly use these variables in my objective script, which also doubles as my scene manager script.



Create A Scene Manager Script

By creating a scene manager script, you can keep the majority of code you use for switching between scenes in one place. This reduces the amount of bugs you might make by modifying code from multiple scripts and overall makes your code much easier to understand. From this script you can also update variables such as the current level number and switch from one scene to the next by using the SceneManager.LoadScene() method. 

In Matter of Great Import, I created a script that manages both the current scene and the objectives since completing all the objectives is how the player advances from one level to the next. Whenever all the objectives have been completed, I access the current level number variable from the static script I made and add 1 to it. After that, I use the SceneManager.LoadScene() method and put SceneManager.GetActiveScene().buildIndex + 1 as the parameter for that method to load the next scene. Generally the next scene is a cutscene, which is why I don't just use the current level number as the parameter in LoadScene(). Also, you can check the indices for all your scenes when you are in the build menu in the editor.


Use Prefabs For Reused Game Objects With Scripts

Any objects in your game that you use in multiple scenes or multiple times in one scene should be made into a prefab, especially ones with scripts. The main reason for this is that while inside a prefab, you can assign to public variables in the scripts on the objects in that prefab and they will remain assigned to when that prefab is put into a scene. This cuts down on time filling up scripts in new scenes immensely, and is probably the best tip in this list.

The most time saving prefab I've made in Matter of Great Import is for a UI element called room zoom. Room zoom shows the player how a room is doing profit wise, condition wise, and what good and bad attributes it is receiving. It only comes up when the player is examining a room they have placed or when they are placing a room, and there is only one object of it in each scene. There are tons of smaller UI elements that make up room zoom that the room zoom script must access, so making it a prefab and assigning to all those variables in the prefab saves around five minutes or more every time I create a new scene. In addition to the room zoom prefab, I've also made a prefab for the overall UI canvas that contains the room zoom object and it has a script on it as well. By making the UI canvas a prefab, I can assign even more variables to its script in its prefab, making for multiple layers of prefabs with preassigned variables.

As you can see, there is a very large number of unity UI objects on this room zoom object, so assigning all the variables while in its prefab is crucial.


Get Matter of Great Import

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.