High Score and Saving Data in Unity: Lessons Learned

I wanted my score and high score text objects to contain text other than solely the score value (in this case it was a simple “Score: ” and “High Score: “). This did lead to a couple issues throughout the debugging process where I realized I didn’t add the extra text when changing/resetting score values, so the extra text would be missing and just the number would be shown. For larger projects, it may be safer and more consistent to make a separate small method for setting a score and/or high score that will add that extra text every time when called (removes a copy/paste step of doing similar actions throughout the scoring script.) And again following a main point from the Board to Bits video, set your key names as string variables within the code so they are easier to consistently call throughout your program.

Saving Data in Unity: PlayerPrefs

Saving Data in Unity: PlayerPrefs – Youtube Link

Youtube tutorial by Board to Bits Games going over the basics of PlayerPrefs in Unity. This shows limitations of PlayerPrefs as something that is mostly useful for storing smaller amounts of data for the player on their system. This also showed a good practice to follow when using key value pairs: set your key as a string variable so that you can call that key variable name every time you need it instead of hoping you type it in exactly correctly every time.

How to Make a High Score in Unity

How to make a HIGH SCORE in Unity by Brackeys

How to make a HIGH SCORE in Unity – Youtube Link

This youtube tutorial from Brackeys shows the bare bones way of getting a high score system started for a game in Unity. It uses the built in PlayerPrefs function to save data to the user’s system.

PlayerPrefs.SetInt and PlayerPrefs.GetInt were used. Playerprefs.SetInt lets you set a key value (which is basically a name for this value when you want to find it) and the actual value to set to this key at the time. Playerprefs.GetInt will return the value stored at the key value given to it, or if nothing is stored within that key location, it will return the default value, which is the second variable value you set when using PlayerPrefs.GetInt.

Altering UI Grid System for Unity 2017

Changing our project from Unity 2018 to 2017 caused a small issue with the UI grid system I created. It relied on a prefab of a UI image object, which 2017 does not play nicely with. They did not take on the positional data assigned to them on instantiation, they would simply keep the transform information of the prefab, so they would all be in the same location which does not create our nice grid.

To get around the need for a prefab, I simply created new gameObjects which then had image components added to them and attached the necessary script since our objects were relatively small and simple. This got around the issue of needing a preexisting object to instantiate in the first place.

These UI image prefabs also had an event trigger attached to them to perform the method to change their color and array value when clicked, so this also needed to be attached or emulated when creating these new objects. Instead of specifically trying to add a pointer click event trigger, I simply added an OnPointerDown method to the script already being attached to the newly created objects that would perform the array value changing method.

Links Referenced for Solution:

UI OnPointerDown Method for Unity
Create UI Elements in Unity

Additional Image Reading Feature for UI Grid Tool

Another feature was added to this tool to help the artists make wall assets for the game, but it also doubles as another option for therapists to create wall poses. The UI grid can intake an image file (a 2D sprite) and output a grid based on the alpha values of individual pixels from this input image. This initially always looked at every pixel, but to help the tool deal with higher resolution (or just larger) images, I also added a resolution factor that would just very simply look at “every x pixels” instead of each individual one. The resolution factor just gets multiplied into the loop looking for pixels.

For example: With resolution factor of 4, the tool will look at pixel numbers: 0 * 4 = 0, 1 * 4 = 4, 2 * 4 = 8, etc.

While this clearly isn’t an amazingly intelligent system, it is a simple way to generally get a roughly similar shape to initially higher resolution images without creating hundreds of thousands of objects.

Creating UI Grid to Build Wall of Objects

Our team wanted to created a tool that could generate walls made of blocks for our “Hole in the Wall” style game. As a health game, we also wanted to have an in game system that a therapist could use to create their own wall objects as well. To satisfy these requests I decided to create a simple UI grid made of images that would tie into a wall generator object in Unity.

Using Unity 2018, I was able to use UI Image prefab objects. The height and width of the wall can be determined in “number of objects”. This will also be reflected in the UI grid object. The user can then click grid elements to either turn them on or off, which will also correspond with a color (white or black). Then when the user clicks the “create wall” button, a wall will be created of 3d objects directly reflecting the UI grid. The wall will have objects where the grid elements are on, and it will just have nothing (a hole) where elements are off.