Search Unity

A Bit of Trouble on Start

Discussion in 'Unity Ads & User Acquisition' started by firativerson, Oct 19, 2015.

  1. firativerson

    firativerson

    Joined:
    Jan 22, 2015
    Posts:
    33
    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.

    Code (CSharp):
    1. void Start () {
    2.         Debug.Log (PlayerPrefs.GetInt ("ADNumber"));
    3.         Advertisement.Initialize ("xxxxx", true);
    4.         Invoke ("showAd", 1f);
    5.  
    6.     }
    7.    
    8.     // Update is called once per frame
    9.     public void showAd(){
    10.         AdCounter=PlayerPrefs.GetInt("ADNumber");
    11.         AdCounter++;
    12.         if ((AdCounter / 5) * 5 == AdCounter) {  // or AdCounter %5==0 doesnt matter
    13.             Advertisement.Show ();
    14.         }
    15.        
    16.         PlayerPrefs.SetInt ("ADNumber", AdCounter);
    17.         }
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Could be that the user setting doesnt exist. Try providing a default value

    AdCounter=PlayerPrefs.GetInt("ADNumber" , 0);
     
  3. firativerson

    firativerson

    Joined:
    Jan 22, 2015
    Posts:
    33
    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.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    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
     
  5. firativerson

    firativerson

    Joined:
    Jan 22, 2015
    Posts:
    33
    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?
     
  6. Salazar

    Salazar

    Joined:
    Sep 2, 2013
    Posts:
    235
    Hello @firativerson ,

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

    Code (CSharp):
    1. public void showAd()
    2.     {
    3.         if(Advertisement.IsReady("defaultVideoAndPictureZone"))
    4.         {
    5.         AdCounter=PlayerPrefs.GetInt("ADNumber");
    6.         AdCounter++;
    7.         if ((AdCounter / 5) * 5 == AdCounter) {  // or AdCounter %5==0 doesnt matter
    8.             Advertisement.Show ();
    9.         }
    10.         PlayerPrefs.SetInt ("ADNumber", AdCounter);
    11.         }
    12.         else
    13.         {
    14.             StartCoroutine(WaitandTryAgain());
    15.         }
    16.     }
    17.    
    18.     void IEnumerator WaitandTryAgain()
    19.     {
    20.         yield return new WaitForSeconds(1f);
    21.         showAd();
    22.     }
    You can make a timeout variable for slower connections. At least your counter couldnt wasted like this.

    Regards,
     
    firativerson likes this.
  7. firativerson

    firativerson

    Joined:
    Jan 22, 2015
    Posts:
    33


    awesome piece of code !!!! thank you Salazar!!
     
    Salazar likes this.