Unity - Saving and Loading Scenes



At the end of the day, when you are done with a fair amount of work, you want to save your progress. In Unity, hitting Ctrl + S will not directly save your project.

Everything in Unity happens in scenes. So does saving and loading; you must save your current work as a scene (.unity extension) in your assets.

Let us try it out. If we hit Ctrl + S and give our scene a name, we will be presented with a new asset in our Assets region. This is the scene file.

Now, let us try and create a new scene. To do so, right click in the Assets and go Create → Scene. Give your new scene a name and hit enter.

In the Editor mode (when the game is not playing), scenes can be loaded into the editor by double-clicking them. Loading a scene with unsaved changes on your current one will prompt you to save or discard your changes.

Your First Script

Importing images and having them stay still in your game is not really going to get you anywhere. It would make a nice picture frame, perhaps, but not a game.

Scripting is imperative to making games in Unity. Scripting is the process of writing blocks of code that are attached like components to GameObjects in the scene. Scripting is one of the most powerful tools at your disposal, and it can make or break a good game.

Scripting in Unity is done through either C# or Unity’s implementation of JavaScript, known as UnityScript (however, with the 2018 cycle, UnityScript is now beginning it’s deprecation phase, so it’s advised not to use it). For the purpose of this series, we will use C#.

To create a new script, right-click in your Assets and go to Create → C# Script. You can also use the Assets tab in the top bar of the engine.

Assets Tab

When you create a new script, a new asset should show up. For the time being, leave the name as it is, and double-click it. Your default IDE should open up along with the script. Let us have a look at what it actually is.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
   // Use this for initialization
   void Start() { 
   }
   // Update is called once per frame
   void Update() {

   }
}

You will see your script name as a class deriving from MonoBehaviour. What is MonoBehaviour? It is a vast library of classes and methods. It helps all the scripts in Unity derive from one way or the other. The more you write scripts in Unity the more you will realize how useful MonoBehaviour actually is.

As we proceed, we have two private scripts that do not have any return types, namely the Start and Update methods. The Start method runs once for the first frame that the gameObject this is used on is active in the scene.

The Update method runs every frame of the game after the Start method. Normally, games in Unity run at 60 FPS or frames per second, which means that the Update method is called 60 times per second while the object is active.

Unity scripting allows you to take advantage of the entirety of the MonoBehaviour class, as well as core C# features such as generic collections, lambda expressions and XML parsing, to name a few. In the next lesson, we will write our first code!

Advertisements