New In-App update for unity and completely free!

Hello I made this tutorial to allow you to make an in-app Update for your Android games in Unity!

This allows you to use the google play core library and it will check if there is an update available for the player, and in this case, force him to download the last version of your game!

Hope you will enjoy it :slight_smile:

Clevereen.

3 Likes

Hi Teacher, thanks for the video!
For me, doesn’t work. Can you help me? Error:

NullReferenceException: Object reference not set to an instance of an object
GoogleUpdate+d__3.MoveNext () (at Assets/mygame/C#-Assets/Scripts-C#/GoogleUpdate.cs:24)

IEnumerator CheckForUpdate()
{
PlayAsyncOperation<AppUpdateInfo, AppUpdateErrorCode> appUpdateInfoOperation = appUpdateManager.GetAppUpdateInfo(); <---------- this is a line 24 with error

// Wait until the asynchronous operation completes.
yield return appUpdateInfoOperation;

if (appUpdateInfoOperation.IsSuccessful)
{
var appUpdateInfoResult = appUpdateInfoOperation.GetResult();
// Check AppUpdateInfo’s UpdateAvailability, UpdatePriority,
// IsUpdateTypeAllowed(), etc. and decide whether to ask the user
// to start an in-app update.

//display if there us an update or not
if (appUpdateInfoResult.UpdateAvailability == UpdateAvailability.UpdateAvailable)
{
inAppStatus.text = UpdateAvailability.UpdateAvailable.ToString();
}
else
{
inAppStatus.text =“No Update Avaiable”;
}

// Creates an AppUpdateOptions defining an immediate in-app
// update flow and its parameters.
var appUpdateOptions = AppUpdateOptions.ImmediateAppUpdateOptions();

StartCoroutine(StartImmediateUpdate(appUpdateInfoResult, appUpdateOptions));

}

}

The complete script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Google.Play.Common;
using Google.Play.AppUpdate;

public class GoogleUpdate : MonoBehaviour
{
[SerializeField] private Text inAppStatus;

AppUpdateManager appUpdateManager = new AppUpdateManager();

// Start is called before the first frame update
void Start()
{
StartCoroutine(CheckForUpdate());
}

IEnumerator CheckForUpdate()
{
PlayAsyncOperation<AppUpdateInfo, AppUpdateErrorCode> appUpdateInfoOperation = appUpdateManager.GetAppUpdateInfo(); ← error here

// Wait until the asynchronous operation completes.
yield return appUpdateInfoOperation;

if (appUpdateInfoOperation.IsSuccessful)
{
var appUpdateInfoResult = appUpdateInfoOperation.GetResult();
// Check AppUpdateInfo’s UpdateAvailability, UpdatePriority,
// IsUpdateTypeAllowed(), etc. and decide whether to ask the user
// to start an in-app update.

//display if there us an update or not
if (appUpdateInfoResult.UpdateAvailability == UpdateAvailability.UpdateAvailable)
{
inAppStatus.text = UpdateAvailability.UpdateAvailable.ToString();
}
else
{
inAppStatus.text =“No Update Avaiable”;
}

// Creates an AppUpdateOptions defining an immediate in-app
// update flow and its parameters.
var appUpdateOptions = AppUpdateOptions.ImmediateAppUpdateOptions();

StartCoroutine(StartImmediateUpdate(appUpdateInfoResult, appUpdateOptions));

}

}

IEnumerator StartImmediateUpdate(AppUpdateInfo appUpdateInfoOp_i, AppUpdateOptions appUpdateOptions_i)
{
// Creates an AppUpdateRequest that can be used to monitor the
// requested in-app update flow.
var startUpdateRequest = appUpdateManager.StartUpdate(
// The result returned by PlayAsyncOperation.GetResult().
appUpdateInfoOp_i,
// The AppUpdateOptions created defining the requested in-app update
// and its parameters.
appUpdateOptions_i);
yield return startUpdateRequest;

// If the update completes successfully, then the app restarts and this line
// is never reached. If this line is reached, then handle the failure (for
// example, by logging result.Error or by displaying a message to the user).
}

/* IEnumerator StartFlexibleUpdate()
{
// Creates an AppUpdateRequest that can be used to monitor the
// requested in-app update flow.
var startUpdateRequest = appUpdateManager.StartUpdate(
// The result returned by PlayAsyncOperation.GetResult().
appUpdateInfoResult,
// The AppUpdateOptions created defining the requested in-app update
// and its parameters.
appUpdateOptions);

while (!startUpdateRequest.IsDone)
{
// For flexible flow,the user can continue to use the app while
// the update downloads in the background. You can implement a
// progress bar showing the download status during this time.
yield return null;
}

} */

}

Did you found bug ? I have same issues… and my game crashed…

Check if you Google Play Installed on your device

I followed the same tutorial, but still pop up not showing and there is no error or crash in my game!

i tried in internal testing and live testing as well. but issue is same pop up not appeared

Hello @waqamers and @leonardoduarteuerj
Sorry for the late reply, I had no notification about it. I Seems that many off you have this issue, it might include myself. I’ll run another test on one of my game and try to understand where is that coming from!

Can any one fixed this problem ?

It never worked. There is another method and actually i did not understand his script because it’s not make a push update to your apk so that why it won’t work. You can actually read official page that have completely different example.

For anyone getting a crash from this script, replace the

private AppUpdateManager appUpdateManager = new AppUpdateManager();

with

private AppUpdateManager appUpdateManager;

This fixed it for me

Hi. Can you please show your full script with this change. Because it’s not work for me at all.

#if UNITY_ANDROID
using System.Collections;
using System.Collections.Generic;
using Google.Play.AppUpdate;
using Google.Play.Common;
using UnityEngine;

public class InAppUpdateManager : MonoBehaviour
{
private AppUpdateManager appUpdateManager;

private static bool checkForUpdate = false;

void Start()
{
#if !UNITY_EDITOR
StartCoroutine(CheckForUpdates());
#endif
}

IEnumerator CheckForUpdates()
{
yield return new WaitForSeconds(5f);

//Only execute once in while app lifecycle
if (checkForUpdate)
yield return null;

checkForUpdate = true;

//Check for an update
PlayAsyncOperation<AppUpdateInfo, AppUpdateErrorCode> appUpdateInfoOperation =
appUpdateManager.GetAppUpdateInfo();

// Wait until the asynchronous operation completes.
yield return appUpdateInfoOperation;

if (appUpdateInfoOperation.IsSuccessful)
{
AppUpdateInfo appUpdateInfoResult = appUpdateInfoOperation.GetResult();

this should fix the crash, however I am now getting an error on the line "appUpdateManager.GetAppUpdateInfo(); Will be working on finding a solution and will update if I have any progress.

1 Like

Thank you very much.
Btw i have no errors in this script try to check out maybe if some of your scripts have conflicts around.
Or maybe this error is caused by #if UNITY_ANDROID you missed to write #endif at the end of line and you missed 2 } }

Exception: Field currentActivity or type signature not found
UnityEngine._AndroidJNIHelper.GetFieldID (System.IntPtr jclass, System.String fieldName, System.String signature, System.Boolean isStatic) (at :0)

i have this error can anybody help

I dont understand how does “private AppUpdateManager appUpdateManager;” ever get assigned an Instance of AppUpdateManager if i dont assign it like this “private AppUpdateManager appUpdateManager = new AppUpdateManager”

In fact it wrong. Your second line is right.
But this method is still not work to me absolutely no response. It’s actually don’t give me any kind of error but just refuse to work.

Hav you got any solution for this? i am facing the same issue.

Seems this still has issues, I tried to put a wrapper on everything and it is still crashing on load, I think it instantiating the the class from google is crashing it but not sure. Still looking for solution.

I am facing the same problem. I noticed there is a step called “Check update priority”, the default priority value is 0, which will neither call a flexible update nor an immediate update. Maybe this is the key point, but I’m not sure.

Yes! that is the point. I tried to set the priority value to “5” with Google Cloud API, then, I can see the update popup and update my app.
If you are a beginner like me, the Google Cloud API is not hard to learn. You can use a software named PostMan and GC API to change the update priority value.