Detect if an app is run the first time?

Is there a way to detect if an app is run the first time?

I have a setting that is saved as a player pref, the issue is when I install the app for the first time and run it, the value defaults to 0. Is there any way to detect if the app has not been run before? What way I can set the value initially.

Thanks,

JL

1 Like

Use PlayerPrefs.

Psuedo Code:

int hasPlayed = PlayerPrefs.GetInt( "HasPlayed");

if( hasPlayed == 0 )
{
   // First Time
   // Initialize...etc

   PlayerPrefs.PutInt( "HasPlayed", 1 );
}
else
{
   // Not First Time
}
3 Likes

You can also check PlayerPrefs.HasKey for one of the usual prefs, if you’re saving anything else. If it exists, then the game has been played before and the preference saved.

–Eric

Hi, How can I detect First run on Android?

This thread already explains how. The code will work for both iOS and Android.

I tried the code works on windows perfectly but it doesn’t work for android.
Did you try it ?

What value does int hasPlayed = PlayerPrefs.GetInt( “HasPlayed”); return on first run?

the returned value is 0 on all runs for android but for windows the value changes from 0 into 1 on first run.

Does it help if you call PlayerPrefs.Save() ?

does this check if the game has ever run before, or if this specific .exe file has been executed before?
I need to check wether the player has “reinstalled” the game, better said redoawnloaded.

This code wold check this also but if the user uninstalled the game completely(even deleted the registry like I do) then you have no chance of chasing him

androids need .Save() explicitly called in my experience.

Then use a server and code all that. Otherwise no.

public class LoadSaveManager : MonoBehaviour
{
    public int IsFirst;

    void Start ()
    {
        IsFirst = PlayerPrefs.GetInt("IsFirst") ;
        if (IsFirst == 0)
        {
            //Do stuff on the first time
            Debug.Log("first run");
            PlayerPrefs.SetInt("IsFirst", 1);
        } else {
            //Do stuff other times
            Debug.Log("welcome again!");
        }
    }
}