Writing Games With Unity 3D in JavaScript and C# – Part 2

by on May 6, 2013 12:30 pm

Welcome back for Part 2! We covered the following topics in Part 1 of this Unity 3D tutorial:

  • Introduction to Unity
  • Introduction to KeyShot
  • Using multiple cameras to implement a background logo
  • JavaScript and C# Implementations (including how to use nested generics in JavaScript and how to call C# scripts from JavaScript)
  • How to use 3D models with axes that don’t line up with normal “Y-up” Unity standards

So if you missed it, be sure to skip over and read it first!

For part 2, we’ll cover the following items:

  • Implementing a high score table using PlayerPrefs
  • How to implement a dialog with text fields to collect user data
  • How to implement cheat codes in Unity

The code is now on GitHub. This will be much more code-intensive than Part 1, so load Unity, click a script, and follow along!

Implementing a high score table using PlayerPrefs

PlayerPrefs can seem fairly limited when you first look at the API. It has 10 methods including DeleteAll(), DeleteKey(), GetFloat(), GetInt(), GetString(), HasKey(), Save(), SetFloat(), SetInt(), and SetString().

One glaring omission is a method that retrieves all keys. To me, that’s like going to a bank, depositing money, and then coming back later to make a withdrawal and being asked what the serial numbers of the bills were.

So, if there is no way to find out what is in PlayerPrefs, how do we save the high scores? We don’t know the user names ahead of time…the scores themselves are no help…hmmmm. Ah, a clue is the HasKey() method. We can ask the object if it has a key. So, if we come up with our own known keys we can use these to save and load the high scores. Let’s look at how that is done.

I decided to use the prefix “playerData” as the key’s known part, and then append an index to that to create a unique key. In scriptSceneManager I defined static strings to use when accessing keys in PlayerPrefs. This keeps the programmer from frustrations caused by mistyping key names, and locates all of the keys in one place so it is easy to keep track of what is being stored.

When 30 seconds have passed or the player has no more lives, saveHighScores() is called. Let’s work through the important bits of that code. Whenever there is a C# or Unity class, method, or property throughout the code, I’ll link it to the respective documentation. If there is no link, that means it is one of my methods.

One of the first things to happen is a call to MakePlayerKey(), which concatenates the data that the user entered in CSV format and returns it as a string. Wait…wasn’t the key “playerData” plus an index? Well…one of this game’s requirements is that any unique player’s data is only saved the first time the game is played (user exercise: remove this limitation :-)). This is because the game was used to enter players in a contest (this was the “shroud of secrecy” mentioned in part 1). So, the PlayerIndex() method searches using the “real” keys (“playerData” plus an index) to try to find the information the user entered. If it is found, the user can still play the game…the score will just be ignored. Since index 0 is not used to store player data, 0 is returned for the keyIndex if the player is found. If the player is not found, the index just past the last player is returned and a List is created to hold the high scores that are about to be read. A List of KeyValuePair<int,string> is used so the score can be separated from the rest of the data to make sorting easier.

    // this data is how we form the key to search for the player
    string playerData = MakePlayerKey();
    int keyIndex = PlayerIndex(playerData);

    if (keyIndex > 0) {
        int maxPlayerIndex = MaxPlayerIndex();
        List<KeyValuePair<int, string>> highScores = new List<KeyValuePair<int, string>>();

Now we don’t just read-in the top 10, although that is all that is displayed. Another requirement was to save all users that played the game. Hey, this is marketing! Don’t worry, Keyhole has no nefarious plans to sell user data – your data is safe with us. So anyway, instead of loading 1-10, we load 1 – maxPLayerIndex. and place them into the list. Since score is the value on the end of each piece of data, it can be grabbed and used separately in the KeyValuePair. Hmm you say…I don’t see any sorting in here. That’s because the list is saved in sorted order, so no full sort ever has to occur :-)

        // read in scores & names
        for (int i = 1; i <= maxPlayerIndex; i++) {
            string currentData = PlayerPrefs.GetString(PREF_PLAYER_DATA + i);
            if (currentData.Length > 0) {
                int currentScore = int.Parse(currentData.Substring(currentData.LastIndexOf(",") + 1));
                KeyValuePair<int,string> highScore = new KeyValuePair<int,string>(currentScore, currentData);

Now that the scores are in the list, the new one needs to be added. This is a simple linear search through the list, comparing the current score with the score in the list. Since the first player to make a score should be higher in the list than any other player with the same score, only greater is used instead of greater or equal. In this way we store equal scores in the order that they were made. If the score was too low to be found, it is added to the end of the list.

        // add current score in sorted position
        playerData += "," + score;
        KeyValuePair<int, string> newScore = new KeyValuePair<int, string>(score, playerData);
        bool playerInserted = false;
        for (int i = 0; i < highScores.Count; i++) {
            if (score > highScores[i].Key) {
                highScores.Insert(i, newScore);
                playerInserted = true;

        if (!playerInserted) {

Now that the new list has been created, the list can be overwritten with the new data, including the new score. The loop switches to a normal 0-Count iteration over the list. If the list was changed, the prefs are persisted. Since Unity supports so many platforms, it takes care of what “persisted” means for each platform. All we have to know is that it worked. If the player was found by the initial search, another temporary pref is set to indicate that fact. This will be used later when the high scores are displayed. Finally, the level is loaded to display the high scores.

        // write out new scores including new player
        for (int i = 0; i < highScores.Count; i++) {
            PlayerPrefs.SetString(PREF_PLAYER_DATA + (i + 1), highScores[i].Value);

    }  else {
        PlayerPrefs.SetString(PREF_DOES_PLAYER_EXIST, "TRUE");


Displaying the high scores can be found in the sceneScreenWin script. I’ll briefly cover that screen.

First, we retrieve the flag to tell if the user exists. Regardless of that fact, we always display the score using the PREF_SCORE temporary pref. The way to tell these prefs are temporary is to look at the scriptScreenGetPlayerInfo script, where the keys are deleted each time the game starts. That screen will be covered in a later section. If the player already existed, the screen shows that the score they just achieved won’t effect the high scores.

bool doesPlayerExist = PlayerPrefs.HasKey(scriptSceneManager.PREF_DOES_PLAYER_EXIST);
float y = 0;
GUI.Label(new Rect(60.0f, y, 290.0f, 50.0f), "Score: " + PlayerPrefs.GetInt(scriptSceneManager.PREF_SCORE));
if (doesPlayerExist) {
    y += 30.0f;
    GUI.Label(new Rect(60.0f, y, 290.0f, 50.0f), "Player found! Score not recorded!");

Now let’s skip down to the scores. For each of the top 10, the data is retrieved. Because 10 players may not have yet played, checking to see if data is present is crucial to prevent runtime scripting errors. If data is present, the stored CSV data is parsed into its respective parts, and then displayed in a formatted list. I didn’t have time to figure out how to display the data using a monospaced font or other cool stuff, but the basics are here and they work.

    for (int i = 1; i <= 10; i++) {
         y += 20.0f;
         string currentData = PlayerPrefs.GetString(scriptSceneManager.PREF_PLAYER_DATA + i);
         int score = 0;
         string firstName = "";
         string lastName = "";
         if (currentData.Length > 0) {
            score = int.Parse(currentData.Substring(currentData.LastIndexOf(",") + 1));
            int index = currentData.IndexOf(",");
            firstName = currentData.Substring(0, index);
            int index2 = currentData.IndexOf(",", index + 1);
            lastName = currentData.Substring(index + 1, index2 - index - 1);

        GUI.Label(new Rect(60.0f, y, 290.0f, y + 20.0f), string.Format("{0,2}. {1,10} : {2}", i, 
                  score, firstName + " " + lastName));


The other screen that shows player data is scriptScreenShowData, which is reached by using the cheat code that we’ll cover later. The code there is fairly obvious so I won’t cover it here.

How to implement a dialog with text fields to collect user data

Now that high scores have been covered, how did the data get there? The answer is that Unity has a rich set of user interface controls built in. I only used a few, with no validations, because this was a quick ‘n dirty game for a conference. When I code a game for publication it will have full validation, and I might do a Part 3 to cover that functionality if anyone is interested.

MonoBehavior is a Unity class that is the base class for every Unity script. It provides a wealth of functionality and is easy to extend. In JavaScript, the C# class generated automatically extends from MonoBehavior – but when coding in C#, the programmer must explicitly extend from it.

The OnGUI() Unity method is called on every frame. It is easy to prove this by adding a print statement to the method and then running the scene. This is why the firstName, lastName, phone, and email fields are defined at the class level instead of inside the method, because we want them to update instead of being re-initialized with each frame.

public class scriptScreenGetPlayerInfo : MonoBehaviour {
    public float buttonWidth = 90.0f;
    public float buttonHeight = 40.0f;
    string firstName = "";
    string lastName = "";
    string phone = "";
    string email = "";

As discussed earlier, when Start() is called the temporary pref keys are removed. Start() is called by Unity once each time a screen is loaded.

Now, to collect user data the GUI.TextField() API is used. A Rect is defined to give the field dimensions on the screen, the data to display is next, and the maximum number of characters to accept is the last parameter. The data entered by the user is returned. Here is one of the field definitions, along with a label to display to the left of it:

    float y = 30.0f;
    GUI.Label(new Rect(10.0f, y, 80.0f, 40.0f), "First Name");
    firstName = GUI.TextField(new Rect(90.0f, y, 200.0f, 30.0f), firstName, 40);

With the data fields displayed, the only thing left is to harvest it. To do this the user clicks on the “Save Info” button. When the button is clicked, GUI.Button() returns true. At that point the fields are saved in prefs and the next screen is loaded.

if (GUI.Button(new Rect(90.0f, y, buttonWidth, buttonHeight), "Save Info")) {
    // here is where you would validate data if it is required
    PlayerPrefs.SetString(scriptSceneManager.PREF_PLAYER_FIRST_NAME, firstName);
    PlayerPrefs.SetString(scriptSceneManager.PREF_PLAYER_LAST_NAME,  lastName);
    PlayerPrefs.SetString(scriptSceneManager.PREF_PLAYER_PHONE, phone);
    PlayerPrefs.SetString(scriptSceneManager.PREF_PLAYER_EMAIL, email);

When the game ends, the fields will be waiting in the temporary pref fields.


How to implement cheat codes in Unity

This was tricky for me because the documentation for the API was very light. I wanted a way to turn on the admin functions that the user would be unlikely to figure out during the short time they were playing the game. Two admin requirements were present for KeyShot.

  1. The high scores needed to be reset just before the game was made available to players so the game could be tested before “real” play began.
  2. The data that had been entered by all users needed to be able to be displayed after the conference was over so it could be harvested. Another way to do this would be to implement a remote call to send the data to a server, but I didn’t have time to set that up. Since the data is stored separately from the game, I can always add that after the conference is over if it is necessary.

The key to implementing cheat codes is the field inputString in the Unity UI class Input. This field holds the key (if any) that was pressed on the current frame, even if there are no input fields on the screen. The documentation for this API is unclear about that and makes it seem like any text that the user types will be held in the field, but this is not the case. The field resets for each frame. So, if a cheat code longer than 1 character is needed (most are…) a little more work needs to be done to make that available for use.

Each time Update() is called, the value in inputString is examined. If a value is present, it is concatenated to the hidden field, which is a class variable so it exists between calls to Update(). Now hidden can be checked to see if the string “key” has been typed. The Contains() method is used so keys can be typed before the cheat code and it will still work. If more than 10 characters have been typed without “key” being present, the hidden field is reset so it doesn’t become too long. Note that if k or e is the 11 character this can prevent the cheat code from working the first time. There is a low chance of that happening, but it certainly can happen. By checking for other strings, any number of cheat codes can be checked for in this manner.

If “key” is present, the numberOfButtons field is increased to 6, indicating that the admin screen is now active.

public class scriptScreenMainMenu : MonoBehaviour {
    string hidden = "";
    int numberOfButtons = 4;

    // Update is called once per frame
    void Update () {
        string input = Input.inputString;
        if (input.Length > 0) {
            hidden += input;

        if (hidden.Contains("key")) {
            numberOfButtons = 6;
        } else if (hidden.Length > 10) {
            hidden = "";

If the numberOfButtons is 6, then the “Show Data” and “Clear Data” buttons are shown and usable.

if (numberOfButtons == 6) {
    y += buttonHeight + 10.0f;
    if (GUI.Button(Rect(10.0f, y, buttonWidth, buttonHeight), "Show Data")) {

    y += buttonHeight + 10.0f;
    if (GUI.Button(Rect(10.0f, y, buttonWidth, buttonHeight), "Clear Data")) {

Here’s the “Show Data” screen, with one score. Basic stuff, but fills the requirement.


So, that wraps it up for Part 2! Make sure to check the code out on GitHub. I truly appreciate your time and interest, and I hope it helps you develop using Unity!

— John Boardman, asktheteam@keyholesoftware.com

  • Share:

3 Responses to “Writing Games With Unity 3D in JavaScript and C# – Part 2”

  1. […] Implementing a high score table using PlayerPrefs (check it out in Part 2!) […]

  2. […] Implementing a high score table using PlayerPrefs (check it out in Part 2!) […]

  3. […] will be building on Part 1 and Part 2 in this Unity 3D tutorial – so if you haven’t read them yet, head on over there first. […]

Leave a Reply

Things Twitter is Talking About
  • New to #JavaScript prototypal inheritance? Here are some notes to help you along the way - http://t.co/NTIDZS6Uhy
    July 5, 2015 at 7:55 AM
  • ICYMI: we've released a demo version of #GrokOla which is open to the public. Try out its features & capabilities - http://t.co/O4ladowmFU
    July 4, 2015 at 3:05 PM
  • Happy 4th of July from the Keyhole team! We hope that you have a happy and safe holiday with your family and friends.
    July 4, 2015 at 9:55 AM
  • Let's talk testing. Here are common challenges #Agile teams face when writing automated tests & how to overcome them: http://t.co/DrKbNZJcE0
    July 3, 2015 at 11:06 AM
  • #GrokOla users get free educational tutorials. But lucky you, we've released some to the public. #JavaScript primer - http://t.co/nIR9XiWY6O
    July 3, 2015 at 10:55 AM
  • Being able to isolate debugging techniques can help make you a better debugger. Here's Time-Oriented #Debugging http://t.co/UplJgP4VzC
    July 2, 2015 at 10:50 AM
  • RT @zachagardner: @zachagardner has declared it is @ChipotleTweets day at @KeyholeSoftware . You have been warned 🐓🐖🐄
    July 2, 2015 at 10:09 AM
  • Current state of random number generation & the differences in how #Java & #JavaScript approach it - http://t.co/5tBKNXnu8T #security
    July 1, 2015 at 2:45 PM
  • Woohoo - 600 followers! Thanks, everyone. We'd love to ask you - what type of tweets / dev content would you like to see more of from us?
    July 1, 2015 at 10:38 AM
  • We would like to welcome Dallas Monson to the team today! Dallas is a Senior Architect focused on UI/UX and #JavaScript. Welcome, Dallas!
    July 1, 2015 at 8:35 AM
  • Good introduction to TypeScript - http://t.co/0N22fVpAHt Plus, how to approach modularization in #TypeScript - http://t.co/wxRWGBj3Uh
    June 30, 2015 at 3:25 PM
  • .@mrbristopher just delivered a new S911 Night Drone to James Hayes, winner of our #kcdc15 giveaway! Congrats, James! http://t.co/RriJIxubH2
    June 30, 2015 at 11:35 AM
  • It feels like primitives could have been left out of the initial implementation of #Java. See why - http://t.co/A8ChCBHXJO
    June 29, 2015 at 4:05 PM
  • Developers in a bounce house! I repeat, developers in a bounce house! We had a blast at our 1st company picnic. Pics: http://t.co/XIqs7ECUst
    June 29, 2015 at 1:40 PM
  • New #SpringBatch tutorial from @jhackett01: Spring Batch – Replacing XML Job Configuration With JavaConfig http://t.co/PmdXnriKQu #java
    June 29, 2015 at 11:46 AM
  • We had such a fun time at the Keyhole company picnic! Pictures to come, including some of our developers in the bounce house. #loveourteam
    June 29, 2015 at 8:41 AM
  • In #JavaScript, how do we harness the power of callbacks without the confusing mess of nested functions? Promises - http://t.co/j1gAJ9hi3D
    June 29, 2015 at 8:40 AM
  • .@zachagardner We are so happy that your family attended! This will definitely need to be repeated every year!
    June 28, 2015 at 8:14 PM
  • Thank you to all on the Keyhole team who came to our first inaugural company picnic! Wonderful food, family and bounce house fun!
    June 28, 2015 at 7:50 PM
  • Debugging is a challenging part of being a programmer. We have a tutorial series to help, with a #JavaScript focus - http://t.co/rfhjJo64P7
    June 27, 2015 at 1:45 PM