Saving progression
Now that we have everything we need to move around with different routes set up, we now need to find a way to save our progress.
Scriptable objects
In order to store our progress, we need to make use of scriptable objects.
A scriptable object is a data container that can be used to save large amounts of data, independent of class instances. So what we can do with this is storing all our level node information and path information into a scriptable object and we can access this data from any script we want to.
To learn more about scriptable objects click on the link below.
Level node data
Let's start off by creating a scriptable object to store our level node information we want our level node to have. For me, I want my level node to have the following data:
Level number
Level name
Position
You can choose whatever you want to save for your level node data, but for this tutorial I want to later be able to display what level we're currently on for now.
What I like to do is inside our scripts folder create a new folder to save our scriptable objects scripts.
Now create a new script and open it and you'll see the following.
Now we need to replace "MonoBehaviour" with "ScriptableObject". This will turn our script into an scriptable object script.
Next we can remove "void Start" and "void Update" since we don't need to use these in our script.
Now we need to add our variables we want to store our desired information we mentioned up above. So in the script that would be the following.
Level number = integer
Level name = string
Position = Vector3
Now we need to be able to create these scriptable objects in our projects and in order to do that we need to make use of the "CreateAssetMenu".
What this does is we can create an asset type of this script by right clicking into our folder, then go to "Create" and then the menu reference where it is stored in.
The line of code we use for this is "[CreateAssetMenu(filename = "", MenuName = "")]. We can add the name of our item we want to create inside file name and the reference of our menu inside menu name. I like to create one main menu name with sub names. So in the end mine looks like this, but yours may vary depending on how you want to name your file and menu name.
And with that, our script is now set up and ready to be used. To create our scriptable object asset we need to back to Unity and right-click into our folder structure and then go to Create --> Worldmap --> LevelInfo.

Now that you've created your asset, you can now also rename your asset then to use the standard asset name you've given it via the script. Once created and you've clicked on it you'll see the following information in the inspector.
We can do here is fill in our level number and level name, we leave the position as is, because we'll set that later. In the end your asset should look something like this.
We can make another one as well for our second level node and give it a level number of two and a different level name.
And with that we have scriptable objects to store our level node data ready for use. Now we need to add these to our level node game objects in our project.
To do this we need to go back to our level node data script and add in a reference to our scriptable object script. Now the difference here is that instead of adding the script as a reference, we can actually set our asset as the reference, since this is basically our script.
Now go back into Unity and set your scriptable object assets to their corresponding level nodes. In the end yours should look something like this.
Now to save the position of the level node we need to back into our level node script and add the function "Private void OnEnable()". This function gets called before our start function, that way we can execute certain code before other code we want to call in our start function, in case we to get certain information before we work with in our start function.
Now inside our on enable function we need to set our vector3 position value equal to the level node position value.
And with that we can save our level nodes position into our scriptable object.
Path data
Now the next scriptable object we want to create is the one that stores our paths unlocked state. To do this we are doing similar steps as we did for our level node data, but with a few changes.
First, we need to create a new script as we did for our level node data. I won't go over the entire creating script progress and changing it to be a scriptable object, since those steps are exactly the same as we did before.
So the script will look something like this.
Now for our path information we only want to store two booleans, one is to store if the path is unlocked or not and the other if it is unlocked for the first time or not.
Now similarly to what we did to create our asset menu for our level data asset, we do the same for our path data asset. The only difference will be the file name and the menu name. So in the end our script should look something like this.
Now create an asset out of your path data script similarly to how we did for our level node data asset.

And with that you've successfully created your path scriptable object asset. Now all we have to do is make a reference for it in our path layout script just like we did with our level node data script.
Now go back into Unity and just like we did with our level node data asset, we need to add our path data asset inside our reference slot.
And with that we've now successfully set up our start towards saving our path unlocked state, but we aren't currently setting these values yet. For that I want to use a path manager script to manage all our paths in our world. That way it's much easier to manage and the coding will look a lot cleaner that way. We'll add this after our final scriptable object asset we're gonna have to create.
World map data
Now that we've got our path data storage ready, we need to have an easier way to manage all of them. For that we're going to make a scriptable object that stores all the path data assets.
To do this we're going to create another script and just like we did for our level node data and path data, we convert our script to be a scriptable object script. The end result of the should look something like this.
First, we need to create a public list of path data to store all our path data assets.
The last thing we need to add is to store our current level reference to position the player back onto the right level node when returning to the world map after returning from a level. This will be implemented later, but lets add this in for now so we don't forget it.
Now all we have to do is create this as an asset into our folder project, just like we did for our level node data and path data assets.
And with that we're all done with creating our scriptable object scripts and assets. Up next we're going to make a path manager to manage all our different paths in our project.
Last updated
