Skip to main content
‹ Back to Tutorials
Intermediate Unity

Unity 3D and Playmaker - Create a Character Selector UI - Part 2 of 2 - Saving PlayerPrefs

Mar 31, 2021
About this tutorial

In this continuation of setting up a character selector, we will look at adding in the ability to Save the PlayerPrefs so that if the user quits the app, when they reopen, their selection is saved. This can be used for all types of selections of characters, players, weapons, etc... EXTREMELY USEFUL

Written Guide

Part two of the character selector takes the UI you can already scroll through and makes the choice stick: clicking a character sets a check mark, updates the active in-scene sprite, and saves the pick with Player Prefs so it survives closing and reopening the app. All of it runs on a second FSM added to the character select manager.

Add a dedicated Player Prefs FSM

Rename the existing movement FSM to character UI movement, then add a second FSM named char player prefs to the same manager. Give it a Start state called Load Character plus three states, char one, char two, and char three — one per selectable character — to hold what should happen when each is chosen. Temporarily disabling the selector mask while building lets you see all characters at once.

Drive the check marks

Each character has its own selected indicator. In char one, use three Activate Game Object actions: turn the first indicator on and the other two off. Copy those into char two and char three, shifting which one is active each time, so exactly one check mark shows at a time. This is purely visual feedback confirming the current selection.

Read the saved character on launch

In Load Character, use Player Prefs Get Int with a count of 1 and the key character, storing the result in a new variable char num. Default it to 1 so a first-time player starts on character one even before anything is saved. Then add three Int Compare actions checking char num against 1, 2, and 3 (equals only), each firing a char one / char two / char three event that routes to the matching state.

Write the choice into Player Prefs

Getting a value isn't the same as saving one — the key doesn't exist until you write it. In each character state, use Player Prefs Set Int (count 1, key character) with the matching value of 1, 2, or 3, and set char num to the same number with Set Int Value. Copy the block across the three states, changing only the number, so selecting a character both records it on the device and updates the working variable.

Wire the buttons and swap the sprite

Add a Character Select state that each character state finishes back into; it listens for the three buttons via UI On Click Event, routing each to its character state — creating a tidy loop where a click sets prefs, marks the choice, and returns to listening. Finally, in each character state add Set Property on the player's Sprite Renderer to swap the in-scene sprite to character one, two, or three. Re-enable the mask when done. Now the selection persists: quit, relaunch, and the last-picked character is still active.

Unity tools by Bliz Studio
Built for our own games — now on the Asset Store

Multi Tag — unlimited hierarchical tags for any GameObject. Hierarchy Pro — style your whole hierarchy with rules. Made by a Unity dev, for Unity devs.

Explore our Unity assets →