Skip to main content
RapidDev - Software Development Agency
firebase-tutorial

How to Use Firebase with Unity

To use Firebase with Unity, download the Firebase Unity SDK from the official GitHub releases, import the packages into your Unity project, and resolve dependencies using the External Dependency Manager (Google Resolver). You can then initialize Firebase and use services like Auth, Firestore, Analytics, and Cloud Messaging directly from C# scripts. Firebase supports both Android and iOS builds from a single Unity project.

What you'll learn

  • How to import the Firebase Unity SDK and resolve dependencies
  • How to initialize Firebase and check readiness in a C# MonoBehaviour
  • How to implement email/password authentication from Unity
  • How to read and write Firestore documents from Unity C# code
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Intermediate7 min read20-30 minFirebase Unity SDK 12+, Unity 2021.3 LTS+, Android and iOS platformsMarch 2026RapidDev Engineering Team
TL;DR

To use Firebase with Unity, download the Firebase Unity SDK from the official GitHub releases, import the packages into your Unity project, and resolve dependencies using the External Dependency Manager (Google Resolver). You can then initialize Firebase and use services like Auth, Firestore, Analytics, and Cloud Messaging directly from C# scripts. Firebase supports both Android and iOS builds from a single Unity project.

Integrating Firebase Services into a Unity Game or App

Firebase provides a dedicated Unity SDK that wraps the native Android and iOS SDKs, giving you access to Auth, Firestore, Realtime Database, Analytics, Cloud Messaging, Crashlytics, and Remote Config from C# scripts. This tutorial covers downloading and importing the SDK, configuring platform-specific settings, initializing Firebase, and using Auth and Firestore in a Unity project.

Prerequisites

  • Unity 2021.3 LTS or later installed with Android and/or iOS build support
  • A Firebase project created in the Firebase Console
  • google-services.json (Android) and/or GoogleService-Info.plist (iOS) downloaded from Firebase Console > Project Settings
  • Basic familiarity with C# scripting in Unity

Step-by-step guide

1

Download and import the Firebase Unity SDK

Download the Firebase Unity SDK from the official GitHub releases page (github.com/firebase/firebase-unity-sdk/releases). The download contains .unitypackage files for each Firebase service. Import only the packages you need — typically FirebaseAuth.unitypackage, FirebaseFirestore.unitypackage, and FirebaseAnalytics.unitypackage. Importing fewer packages reduces build size and dependency conflicts.

typescript
1// Import via Unity Editor:
2// Assets > Import Package > Custom Package
3// Select FirebaseAuth.unitypackage
4// Select FirebaseFirestore.unitypackage
5// Select FirebaseAnalytics.unitypackage

Expected result: Firebase plugins appear under Assets/Firebase in your Unity project. The External Dependency Manager plugin is also imported.

2

Add platform configuration files

Place google-services.json in your Unity project's Assets folder for Android builds. For iOS, place GoogleService-Info.plist in Assets. These files contain your Firebase project's API keys and configuration. The Firebase SDK reads them automatically during initialization.

typescript
1// File locations:
2// Assets/google-services.json (Android)
3// Assets/GoogleService-Info.plist (iOS)

Expected result: The config files are in the Assets root directory and recognized by the Firebase SDK.

3

Resolve dependencies with the External Dependency Manager

The Firebase Unity SDK uses the External Dependency Manager for Unity (EDM4U) to pull native Android and iOS dependencies. After importing Firebase packages, trigger resolution manually. For Android, this downloads AAR files. For iOS, it configures CocoaPods.

typescript
1// Unity Editor menu:
2// Assets > External Dependency Manager > Android Resolver > Force Resolve
3// Assets > External Dependency Manager > iOS Resolver > Install Cocoapods

Expected result: Android dependencies are downloaded to Assets/Plugins/Android. iOS Podfile is generated for CocoaPods.

4

Initialize Firebase and check dependencies

Create a C# script that initializes Firebase when the game starts. The CheckAndFixDependenciesAsync method verifies that all required native dependencies are available on the device. This is critical because Firebase may fail silently on devices without Google Play Services.

typescript
1// Scripts/FirebaseInit.cs
2using UnityEngine;
3using Firebase;
4using Firebase.Extensions;
5
6public class FirebaseInit : MonoBehaviour
7{
8 public static bool IsReady { get; private set; }
9
10 void Start()
11 {
12 FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
13 {
14 if (task.Result == DependencyStatus.Available)
15 {
16 Debug.Log("Firebase initialized successfully");
17 IsReady = true;
18 }
19 else
20 {
21 Debug.LogError($"Firebase dependency error: {task.Result}");
22 }
23 });
24 }
25}

Expected result: Firebase initializes on app start and logs a success message. IsReady is true when services are available.

5

Implement email/password authentication

Use Firebase Auth to create accounts and sign in users. All Firebase Auth calls in Unity return Tasks that you can chain with ContinueWithOnMainThread to safely update Unity UI from the callback. Always check for exceptions in the task result.

typescript
1// Scripts/AuthManager.cs
2using UnityEngine;
3using Firebase.Auth;
4using Firebase.Extensions;
5
6public class AuthManager : MonoBehaviour
7{
8 private FirebaseAuth auth;
9
10 void Start()
11 {
12 auth = FirebaseAuth.DefaultInstance;
13 }
14
15 public void Register(string email, string password)
16 {
17 auth.CreateUserWithEmailAndPasswordAsync(email, password)
18 .ContinueWithOnMainThread(task =>
19 {
20 if (task.Exception != null)
21 {
22 Debug.LogError($"Registration failed: {task.Exception.Message}");
23 return;
24 }
25 Debug.Log($"User created: {task.Result.User.UserId}");
26 });
27 }
28
29 public void SignIn(string email, string password)
30 {
31 auth.SignInWithEmailAndPasswordAsync(email, password)
32 .ContinueWithOnMainThread(task =>
33 {
34 if (task.Exception != null)
35 {
36 Debug.LogError($"Sign-in failed: {task.Exception.Message}");
37 return;
38 }
39 Debug.Log($"Signed in: {task.Result.User.Email}");
40 });
41 }
42}

Expected result: Calling Register creates a new Firebase user. Calling SignIn authenticates and returns user details.

6

Read and write Firestore documents

Use the Firestore SDK to save and retrieve game data like player profiles, scores, or inventory. Firestore in Unity uses dictionaries for document data. Always ensure Firebase is initialized before making Firestore calls.

typescript
1// Scripts/FirestoreManager.cs
2using UnityEngine;
3using Firebase.Firestore;
4using Firebase.Extensions;
5using System.Collections.Generic;
6
7public class FirestoreManager : MonoBehaviour
8{
9 private FirebaseFirestore db;
10
11 void Start()
12 {
13 db = FirebaseFirestore.DefaultInstance;
14 }
15
16 public void SavePlayerScore(string playerId, int score)
17 {
18 var data = new Dictionary<string, object>
19 {
20 { "score", score },
21 { "updatedAt", FieldValue.ServerTimestamp }
22 };
23
24 db.Collection("players").Document(playerId).SetAsync(data)
25 .ContinueWithOnMainThread(task =>
26 {
27 if (task.Exception != null)
28 Debug.LogError($"Save failed: {task.Exception.Message}");
29 else
30 Debug.Log("Score saved");
31 });
32 }
33
34 public void GetPlayerScore(string playerId)
35 {
36 db.Collection("players").Document(playerId).GetSnapshotAsync()
37 .ContinueWithOnMainThread(task =>
38 {
39 if (task.Result.Exists)
40 {
41 var data = task.Result.ToDictionary();
42 Debug.Log($"Score: {data["score"]}");
43 }
44 });
45 }
46}

Expected result: Player scores are saved to and retrieved from Firestore documents.

Complete working example

Scripts/FirebaseInit.cs
1// Scripts/FirebaseInit.cs
2using UnityEngine;
3using Firebase;
4using Firebase.Auth;
5using Firebase.Firestore;
6using Firebase.Extensions;
7using System.Collections.Generic;
8
9public class FirebaseInit : MonoBehaviour
10{
11 public static bool IsReady { get; private set; }
12 public static FirebaseAuth Auth { get; private set; }
13 public static FirebaseFirestore Db { get; private set; }
14
15 void Start()
16 {
17 FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
18 {
19 if (task.Result == DependencyStatus.Available)
20 {
21 Auth = FirebaseAuth.DefaultInstance;
22 Db = FirebaseFirestore.DefaultInstance;
23 IsReady = true;
24 Debug.Log("Firebase initialized successfully");
25
26 // Listen for auth state changes
27 Auth.StateChanged += OnAuthStateChanged;
28 }
29 else
30 {
31 Debug.LogError($"Could not resolve Firebase dependencies: {task.Result}");
32 }
33 });
34 }
35
36 private void OnAuthStateChanged(object sender, System.EventArgs e)
37 {
38 FirebaseUser user = Auth.CurrentUser;
39 if (user != null)
40 Debug.Log($"User signed in: {user.UserId}");
41 else
42 Debug.Log("User signed out");
43 }
44
45 void OnDestroy()
46 {
47 if (Auth != null)
48 Auth.StateChanged -= OnAuthStateChanged;
49 }
50}

Common mistakes when using Firebase with Unity

Why it's a problem: Mixing Firebase Unity SDK versions, causing dependency resolution failures and build errors

How to avoid: Always download all Firebase .unitypackage files from the same SDK release. Delete old Firebase plugins before importing a new version.

Why it's a problem: Calling Firebase APIs before CheckAndFixDependenciesAsync completes, resulting in null reference exceptions

How to avoid: Gate all Firebase calls behind an IsReady check or wait for the initialization callback before accessing Auth, Firestore, or other services.

Why it's a problem: Using package name in Unity that does not match the app registered in Firebase Console

How to avoid: Ensure your Unity Player Settings > Package Name matches exactly the Android/iOS app you registered in Firebase Console > Project Settings.

Best practices

  • Import only the Firebase packages you actually use to minimize build size and dependency conflicts
  • Always call CheckAndFixDependenciesAsync before using any Firebase service
  • Use ContinueWithOnMainThread for all Firebase callbacks to safely update Unity UI and GameObjects
  • Keep google-services.json and GoogleService-Info.plist out of public repositories
  • Test on real devices since the Firebase Unity SDK does not fully work in the Unity Editor for all services
  • For games with complex backend requirements like matchmaking, leaderboards, and real-time state sync, RapidDev can help architect the Firebase infrastructure
  • Use Firebase Analytics to track player behavior and Firebase Crashlytics to monitor crash reports in production

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I am building a Unity game and want to use Firebase for authentication and saving player data to Firestore. Show me how to initialize Firebase in Unity, implement email/password login with FirebaseAuth, and save/load player scores from Firestore using C# MonoBehaviours.

Firebase Prompt

Set up Firebase in my Unity project for Android. I need email/password authentication and Firestore for storing player profiles with fields: displayName, score, and lastLogin. Show the initialization script, auth manager, and Firestore read/write code in C# using ContinueWithOnMainThread.

Frequently asked questions

Does Firebase work in the Unity Editor?

Some services like Auth and Firestore work in the Unity Editor on desktop, but with limitations. Analytics, Crashlytics, and Cloud Messaging require a real Android or iOS device. Always test Firebase features on actual devices before release.

Can I use Firebase with Unity WebGL builds?

The Firebase Unity SDK does not support WebGL builds. For web-based Unity games, you would need to use the Firebase JavaScript SDK through a JavaScript plugin bridge, which adds significant complexity.

How do I handle Firebase on both Android and iOS from one Unity project?

Place both google-services.json (Android) and GoogleService-Info.plist (iOS) in your Assets folder. The Firebase SDK automatically uses the correct config file based on the build target. Register both platforms in the Firebase Console.

What Unity versions are supported by Firebase?

Firebase Unity SDK 12+ supports Unity 2021.3 LTS and later. Earlier Unity versions may work but are not officially supported. Always check the Firebase Unity SDK release notes for the latest compatibility information.

How do I update the Firebase Unity SDK?

Delete all existing Firebase plugins from your Assets folder (Assets/Firebase, Assets/Plugins containing Firebase files), then import the new .unitypackage files. Run Force Resolve on the External Dependency Manager after updating.

Is Firebase free for Unity games?

Firebase Auth, Analytics, Crashlytics, Cloud Messaging, and Remote Config are free regardless of scale. Firestore and Cloud Functions are free within Spark plan limits (50K reads/day, 20K writes/day). The Blaze plan charges only for usage above the free tier.

Can I use Realtime Database instead of Firestore in Unity?

Yes. The Firebase Unity SDK supports both Firestore and Realtime Database. RTDB is a good choice for real-time game state with low-latency requirements. Import FirebaseDatabase.unitypackage instead of FirebaseFirestore.unitypackage.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.