A Bit of Trouble on Start

Hi all,
My application consist of one scene with an option of user to refresh the screen i simply call LoadScene again. I want to show ad on every 5th loading, it works fine on refresh screen . However it never works on a new app start up. It works on unity but it doesnt work on my android. Here is my code, any help would be great.

void Start () {
        Debug.Log (PlayerPrefs.GetInt ("ADNumber"));
        Advertisement.Initialize ("xxxxx", true);
        Invoke ("showAd", 1f); 

    }
   
    // Update is called once per frame
    public void showAd(){
        AdCounter=PlayerPrefs.GetInt("ADNumber");
        AdCounter++;
        if ((AdCounter / 5) * 5 == AdCounter) {  // or AdCounter %5==0 doesnt matter
            Advertisement.Show ();
        }
       
        PlayerPrefs.SetInt ("ADNumber", AdCounter);
        }

Could be that the user setting doesnt exist. Try providing a default value

AdCounter=PlayerPrefs.GetInt(“ADNumber” , 0);

The problem is splash screen i think. When I called the ads after 3 seconds it worked right after splash screen.
Somehow I need to check when the app is opening and Invoke the ad after 3 seconds,
and on refresh screen i will invoke the ad after 0.5 seconds. Because popping up the ad late after refresh is not very user friendly.

James thanks for your reply, but setting AdCounter to 0 on every start method would never show any ads., I am not calling showAd() method from anywhere else except the start of this script.

You misunderstand my code.

Its saying, get me this UserPref, but if it doesn’t exist (because why would it first time) give me a default value.

If you’re still having trouble, I would look at attaching the debugger to your droid and reading the Debug.Log output

adb logcat -s Unity

James please let others answer because your are not making sense to me.
Let me restate my question : How do I start my app right after splash screen?

Hello @firativerson ,

You should try a coroutine that checks if the ad is ready to show. Until than dont let the game to start.

public void showAd()
    {
        if(Advertisement.IsReady("defaultVideoAndPictureZone"))
        {
        AdCounter=PlayerPrefs.GetInt("ADNumber");
        AdCounter++;
        if ((AdCounter / 5) * 5 == AdCounter) {  // or AdCounter %5==0 doesnt matter
            Advertisement.Show ();
        }
        PlayerPrefs.SetInt ("ADNumber", AdCounter);
        }
        else
        {
            StartCoroutine(WaitandTryAgain());
        }
    }
   
    void IEnumerator WaitandTryAgain()
    {
        yield return new WaitForSeconds(1f);
        showAd();
    }

You can make a timeout variable for slower connections. At least your counter couldnt wasted like this.

Regards,

1 Like

awesome piece of code !!! thank you Salazar!!

1 Like