Friday, September 11, 2015

Saving Data Between Scenes in Unity

This tutorial assumes basic knowledge of Unity Engine. If you don’t have your own project set up, you can freely grab the project example linked here. You will also find a download of the completed project at the end of this article.

If you’re struggling with saving data between two scenes, this is the tutorial for you.

unity-logo


Starting Point

Download the example project:

[GitHub Repository link]
[ZIP Download]

The logic

Unity is a game engine with its own philosophy. Even though there are quite a few alternatives, it’s quite unique in the way it handles the building blocks of any game - game objects, scenes, code, scene graph. And by unique, I mean how easy it is to understand it.

If you’ve tried giving Unity a test run because it’s free to download, you were probably introduced to how its scripting is implemented. „Scripts“, written in either C#, JavaScript (or, since Unity 5, UnityScript) or Boo language, are components that are attached to any game object. From within a script, you’re free to access and manipulate the object the script is attached to, or any linked objects. It’s quite intuitive, easy to use, and fun to build with.

Supposedly, then you tried building a second level; say your first level was the interior of the house, and then your second level is outside of the house. The transition is using the door to load the next scene.

Here’s the core problem we’ll be tackling today. Each level within the Unity engine is called a „Scene“. You can edit the Scene using the graphical editor however you like. You can transition the scene using a single line of code (that is triggered, perhaps, by the player touching the door, or using an object, etc). Each scene has objects, which have „components“.

Basic representation of the scene

Generic „Objects“ represent what ever else makes up your level (meshes, triggers, particles, etc)

Each scene is built in its initial state. Transitioning the scene to another one means the new scene will be loaded at its initial state (naturally). But what about the player’s statistics, for example his ammo count, or experience, or inventory?

How do we preserve the data, when we can only write code in „Scripts“ - components of Game Objects - which just get destroyed during scene transitions?

Continue reading %Saving Data Between Scenes in Unity%


by Zdravko Jakupec via SitePoint

No comments:

Post a Comment