Interested in speaking at MongoDB World 2022? Click here to become a speaker.
HomeLearnHow-toBuild an Infinite Runner Game with Unity and the Realm Unity SDK

Build an Infinite Runner Game with Unity and the Realm Unity SDK

Published: Mar 04, 2021

  • Realm
  • C#
  • Unity

By Nic Raboy

Rate this article

Did you know that MongoDB has a Realm SDK for the Unity game development framework that makes working with game data effortless? It's currently an alpha release, but you can already start using it to build persistence into your cross platform gaming projects.

A popular game template for the past few years has been in infinite runner style games. Games such as Temple Run and Subway Surfers have had many competitors, each with their own spin on the subject. If you're unfamiliar with the infinite runner concept, the idea is that you have a player that can move horizontally to fixed positions. As the game progresses, obstacles and rewards enter the scene. The player must dodge or obtain depending on the object and this happens until the player collides with an obstacle. As time progresses, the game generally speeds up to make things more difficult.

While the game might sound complicated, there's actually a lot of repetition.

In this tutorial, we'll look at how to create our own infinite runner game with Unity and C#. We'll look at important concepts such as object pooling and collision, as well as data persistence using the Realm SDK for Unity.

To get an idea of what we want to build, check out the following animated image:

Infinite Runner with Realm SDK for Unity

As you can see in the above image, we have simple shapes as well as cake. The score increases as time increases or when cake is obtained. The level restarts when you collide with an obstacle and depending on what your score was, it could now be the new high score.

#The Requirements

There are a few requirements, some of which will change once the Realm SDK for Unity becomes a stable release.

This tutorial might work with earlier versions of Unity. However, 2020.2.4f1 is the version that I'm using. As of right now, the Realm SDK for Unity is only available as a tarball through GitHub rather than through the Unity Asset Store. For now, you'll have to dig through the releases on GitHub.

#Creating the Game Objects for the Player, Obstacles, and Rewards

Even though there are a lot of visual components moving around on the screen, there's not a lot happening behind the scenes. There are three core visual objects that make up this game example.

We have the player, the obstacles, and the rewards, which we're going to interchangeably call cake. Each game object will have the same components, but different scripts. We'll add the components here, but create the scripts later.

Within your project, create the three different game objects. To start, each game object will be empty.

Game Objects in Infinite Runner Game

Rather than working with all kinds of fancy graphics, create a 1x1 pixel image that is white. We're going to use it for all of our game objects, just giving them a different color or size.

Each game object should have a Sprite Renderer, Rigidbody 2D, and a Box Collider 2D component attached. The Sprite Render can use the 1x1 pixel graphic or one of your choosing. For the Rigidbody 2D, make sure the Body Type is Kinematic on all game objects because we won't be using things like gravity. Likewise, make sure the Is Trigger is enabled for each of the Box Collider 2D components.

We'll be adding more as we go along, but for now, we have a starting point.

#Creating an Object to Represent the Game Controller

There are a million different ways to create a great game with Unity. However, for this game, we're going to not rely on any particular visually rendered object for managing the game itself. Instead, we're going to create a game object responsible for game management.

Add an empty game object to your scene titled GameController. While we won't be doing anything with it now, we'll be attaching scripts to it for managing the object pool and the score.

#Adding Logic to the Game Objects Within the Scene with C# Scripts

With the three core game objects (player, obstacle, reward) in the scene, we need to give each of them some game logic. Let's start with the logic for the obstacle and reward since they are similar.

The idea behind the obstacle and reward is that they are constantly moving down from the top of the screen. As they become visible, the position along the x-axis is randomized. As they fall off the screen, the object is disabled and eventually reset.

Create an Obstacle.cs file with the following C# code:

1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
5public class Obstacle : MonoBehaviour {
7 public float movementSpeed;
9 private float[] _fixedPositionX = new float[] { -8.0f, 0.0f, 8.0f };
11 void OnEnable() {
12 int randomPositionX = Random.Range(0, 3);
13 transform.position = new Vector3(_fixedPositionX[randomPositionX], 6.0f, 0);
14 }
16 void Update() {
17 transform.position += Vector3.down * movementSpeed * Time.deltaTime;
18 if(transform.position.y < -5.25) {
19 gameObject.SetActive(false);
20 }
21 }

In the above code, we have fixed position possibilities. When the game object is enabled, we randomly choose from one of the possible fixed positions and update the overall position of the game object.

For every frame of the game, the position of the game object falls down on the y-axis. If the object reaches a certain position, it is then disabled.

Similarly, create a Cake.cs file with the following code:

1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
5public class Cake : MonoBehaviour {
7 public float movementSpeed;
9 private float[] _fixedPositionX = new float[] { -8.0f, 0.0f, 8.0f };
11 void OnEnable() {
12 int randomPositionX = Random.Range(0, 3);
13 transform.position = new Vector3(_fixedPositionX[randomPositionX], 6.0f, 0);
14 }
16 void Update() {
17 transform.position += Vector3.down * movementSpeed * Time.deltaTime;
18 if (transform.position.y < -5.25) {
19 gameObject.SetActive(false);
20 }
21 }
23 void OnTriggerEnter2D(Collider2D collider) {
24 if (collider.gameObject.tag == "Player") {
25 gameObject.SetActive(false);
26 }
27 }

The above code should look the same with the exception of the OnTriggerEnter2D function. In the OnTriggerEnter2D function, we have the following code:

1void OnTriggerEnter2D(Collider2D collider) {
2 if (collider.gameObject.tag == "Player") {
3 gameObject.SetActive(false);
4 }

If the current reward game object collides with another game object and that other game object is tagged as being a "Player", then the reward object is disabled. We'll handle the score keeping of the consumed reward elsewhere.

Make sure to attach the Obstacle and Cake scripts to the appropriate game objects within your scene.

With the obstacles and rewards out of the way, let's look at the logic for the player. Create a Player.cs file with the following code:

1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEngine.SceneManagement;
6public class Player : MonoBehaviour {
8 public float movementSpeed;
10 void Update() {
11 if(Input.GetKey(KeyCode.LeftArrow)) {
12 transform.position += Vector3.left * movementSpeed * Time.deltaTime;
13 } else if(Input.GetKey(KeyCode.RightArrow)) {
14 transform.position += Vector3.right * movementSpeed * Time.deltaTime;
15 }
16 }
18 void OnTriggerEnter2D(Collider2D collider) {
19 if(collider.gameObject.tag == "Obstacle") {
20 // Handle Score Here
21 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
22 } else if(collider.gameObject.tag == "Cake") {
23 // Handle Score Here
24 }
25 }

The Player.cs file will change in the future, but for now, we can move the player around based on the arrow keys on the keyboard. We are also looking at collisions with other objects. If the player object collides with an object tagged as being an obstacle, then the goal is to change the score and restart the scene. Otherwise, if the player object collides with an object tagged as being "Cake", which is a reward, then the goal is to just change the score.

Make sure to attach the Player script to the appropriate game object within your scene.

#Pooling Obstacles and Rewards with Performance-Maximizing Object Pools

As it stands, when an obstacle falls off the screen, it becomes disabled. As a reward is collided with or as it falls off the screen, it becomes disabled. In an infinite runner, we need those obstacles and rewards to be constantly resetting to look infinite. While we could just destroy and instantiate as needed, that is a performance-heavy task. Instead, we should make use of an object pool.

The idea behind an object pool is that you instantiate objects when the game starts. The number you instantiate is up to you. Then, while the game is being played, objects are pulled from the pool if they are available and when they are done, they are added back to the pool. Remember the enabling and disabling of our objects in the obstacle and reward scripts? That has to do with pooling.

Ages ago, I had written a tutorial around object pooling, but we'll explore it here as a refresher. Create an ObjectPool.cs file with the following code:

1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
5public class ObjectPool : MonoBehaviour
8 public static ObjectPool SharedInstance;
10 private List<GameObject> pooledObstacles;
11 private List<GameObject> pooledCake;
12 public GameObject obstacleToPool;
13 public GameObject cakeToPool;
14 public int amountToPool;
16 void Awake() {
17 SharedInstance = this;
18 }
20 void Start() {
21 pooledObstacles = new List<GameObject>();
22 pooledCake = new List<GameObject>();
23 GameObject tmpObstacle;
24 GameObject tmpCake;
25 for(int i = 0; i < amountToPool; i++) {
26 tmpObstacle = Instantiate(obstacleToPool);
27 tmpObstacle.SetActive(false);
28 pooledObstacles.Add(tmpObstacle);
29 tmpCake = Instantiate(cakeToPool);
30 tmpCake.SetActive(false);
31 pooledCake.Add(tmpCake);
32 }
33 }
35 public GameObject GetPooledObstacle() {
36 for(int i = 0; i < amountToPool; i++) {
37 if(pooledObstacles[i].activeInHierarchy == false) {
38 return pooledObstacles[i];
39 }
40 }
41 return null;
42 }
44 public GameObject GetPooledCake() {
45 for(int i = 0; i < amountToPool; i++) {
46 if(pooledCake[i].activeInHierarchy == false) {
47 return pooledCake[i];
48 }
49 }
50 return null;
51 }

If the code looks a little familiar, a lot of it was taken from the Unity educational resources, particularly Introduction to Object Pooling.

The ObjectPool class is meant to be a singleton instance, meaning that we want to use the same pool regardless of where we are and not accidentally create numerous pools. We start by initializing each pool, which in our example is a pool of obstacles and a pool of rewards. For each object in the pool, we initialize them as disabled. The instantiation of our objects will be done with prefabs, but we'll get to that soon.

With the pool initialized, we can make use of the GetPooledObstacle or GetPooledCake methods to pull from the pool. Remember, items in the pool should be disabled. Otherwise, they are considered to be in use. We loop through our pools to find the first object that is disabled and if none exist, then we return null.

Alright, so we have object pooling logic and need to fill the pool. This is where the object prefabs come in.

As of right now, you should have an Obstacle game object and a Cake game object in your scene. These game objects should have various physics and collision-related components attached, as well as the logic scripts. Create a Prefabs directory within your Assets directory and then drag each of the two game objects into that directory. Doing this will convert them from a game object in the scene to a reusable prefab.

With the prefabs in your Prefabs directory, delete the obstacle and reward game objects from your scene. We're going to add them to the scene via our object pooling script, not through the Unity UI.

You should have the ObjectPool script completed. Make sure you attach this script to the GameController game object. Then, drag each of your prefabs into the public variables of that script in the inspector for the GameController game object.

Just like that, your prefabs will be pooled at the start of your game. However, just because we are pooling them doesn't mean we are using them. We need to create another script to take objects from the pool.

Create a GameController.cs file and include the following C# code:

1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
5public class GameController : MonoBehaviour {
7 public float obstacleTimer = 2;
8 public float timeUntilObstacle = 1;
9 public float cakeTimer = 1;
10 public float timeUntilCake = 1;
12 void Update() {
13 timeUntilObstacle -= Time.deltaTime;
14 timeUntilCake -= Time.deltaTime;
15 if(timeUntilObstacle <= 0) {
16 GameObject obstacle = ObjectPool.SharedInstance.GetPooledObstacle();
17 if(obstacle != null) {
18 obstacle.SetActive(true);
19 }
20 timeUntilObstacle = obstacleTimer;
21 }
22 if(timeUntilCake <= 0) {
23 GameObject cake = ObjectPool.SharedInstance.GetPooledCake();
24 if(cake != null) {
25 cake.SetActive(true);
26 }
27 timeUntilCake = cakeTimer;
28 }
29 }

In the above code, we are making use of a few timers. We're creating timers to determine how frequently an object should be taken from the object pool.

When the timer indicates we are ready to take from the pool, we use the GetPooledObstacle or GetPooledCake methods, set the object taken as enabled, and then reset the timer. Each instantiated prefab has the logic script attached, so once the object is enabled, it will start falling from the top of the screen.

To activate this script, make sure to attach it to the GameController game object within the scene.

#Persisting Game Scores with the Realm SDK for Unity

If you ran the game as of right now, you'd be able to move your player around and collide with obstacles or rewards that continuously fall from the top of the screen. There's no concept of score-keeping or data persistence in the game up until this point.

Including Realm in the game can be broken into two parts. For now, it is three parts due to needing to manually add the dependency to your project, but two parts will be evergreen.

From the Realm .NET releases, find the latest release that includes Unity. For this tutorial, I'm using the realm.unity.bundle-10.1.1.tgz file.

In Unity, click Window -> Package Manager and choose to Add package from tarball..., then find the Realm SDK that you had just downloaded.

Unity Manually Add Package

It may take a few minutes to import the SDK, but once it's done, we can start using it.

Before we start adding code, we need to be able to display our score information to the user. In your Unity scene, add three Text game objects: one for the high score, one for the current score, and one for the amount of cake or rewards obtained. We'll be using these game objects soon.

Let's create a PlayerStats.cs file and add the following C# code:

1using Realms;
3public class PlayerStats : RealmObject {
5 [PrimaryKey]
6 public string Username { get; set; }
8 public RealmInteger<int> Score { get; set; }
10 public PlayerStats() {}
12 public PlayerStats(string Username, int Score) {
13 this.Username = Username;
14 this.Score = Score;
15 }

The above code represents an object within our Realm data store. For our example, we want the high score for any given player to be in our Realm. While we won't have multiple users in our example, the foundation is there.

To use the above RealmObject, we'll want to create another script. Create a Score.cs file and add the following code:

1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEngine.UI;
5using Realms;
7public class Score : MonoBehaviour {
9 private Realm _realm;
10 private PlayerStats _playerStats;
11 private int _cake;
13 public Text highScoreText;
14 public Text currentScoreText;
15 public Text cakeText;
17 void Start() {
18 _realm = Realm.GetInstance();
19 _playerStats = _realm.Find<PlayerStats>("nraboy");
20 if(_playerStats is null) {
21 _realm.Write(() => {
22 _playerStats = _realm.Add(new PlayerStats("nraboy", 0));
23 });
24 }
25 highScoreText.text = "HIGH SCORE: " + _playerStats.Score.ToString();
26 _cake = 0;
27 }
29 void OnDisable() {
30 _realm.Dispose();
31 }
33 void Update() {
34 currentScoreText.text = "SCORE: " + (Mathf.Floor(Time.timeSinceLevelLoad) + _cake).ToString();
35 cakeText.text = "CAKE: " + _cake;
36 }
38 public void CalculateHighScore() {
39 int snapshotScore = (int)Mathf.Floor(Time.timeSinceLevelLoad) + _cake;
40 if(_playerStats.Score < snapshotScore) {
41 _realm.Write(() => {
42 _playerStats.Score = snapshotScore;
43 });
44 }
45 }
47 public void AddCakeToScore() {
48 _cake++;
49 }

In the above code, when the Start method is called, we get the Realm instance and do a find for a particular user. If the user doesn't exist, we create a new one, at which point we can use our Realm like any other object in our application.

When we decide to call the CalculateHighScore method, we do a check to see if the new score should be saved. In this example, we are using the rewards as a multiplier to the score.

If you've never used Realm before, the Realm SDK for Unity uses the same API as the .NET SDK. You can learn more about how to use it in the getting started guide. You can also swing by the community to get additional help.

So, we have the Score class. This script should be attached to the GameController game object and each of the Text game objects should be dragged into the appropriate areas using the inspector.

We're not done yet. Remember, our Player.cs file needed to update the score. Before we open our class, make sure to drag the GameController into the appropriate area of the Player game object using the Unity inspector.

Open the Player.cs file and add the following to the OnTriggerEnter2D method:

1void OnTriggerEnter2D(Collider2D collider) {
2 if(collider.gameObject.tag == "Obstacle") {
3 score.CalculateHighScore();
4 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
5 } else if(collider.gameObject.tag == "Cake") {
6 score.AddCakeToScore();
7 }

When running the game, not only will we have something playable, but the score should update and persist depending on if we've failed at the level or not.

Infinite Runner with Realm SDK for Unity

The above image is a reminder of what we've built, minus the graphic for the cake.


You just saw how to create an infinite runner type game with Unity and C# that uses the MongoDB Realm SDK for Unity when it comes to data persistence. Like previously mentioned, the Realm SDK is currently an alpha release, so it isn't a flawless experience and there are features missing. However, it works great for an example like we saw here.

If you're interested in checking out this project, it can be found on GitHub. There's also a video version of this tutorial, an on-demand live-stream, which can be found below.

As a fun fact, this infinite runner example wasn't my first attempt at one. I built something similar a long time ago and it was quite fun.

If you have questions, please head to our developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.

Rate this article
MongoDB logo
© 2021 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.