• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dhar174 · Apr 04, 2017 at 05:15 AM · scripting problem

ADS WORK IN EDITOR BUT NOT AFTER BUILDING?

When in the Unity editor, test ads (Unity Ads) run fine. However, after publishing (I had assumed all was well), I discovered no ads were playing, causing players to get stuck in the preloading scene (I built a "preloading" scene before the first level/scene where I placed a script that runs the ad. When the ad is done, the first scene is loaded).

However, it seems the player was getting stuck in the preloading scene because no ad was played. I patched an update to the ad script, which loads the first scene in the case the ad fails. However, the problem remains that no ads play, despite working in the editor.

I am including the script I am using to run the ads in the preloading scene. I am unsure what to do as I am uncertain whether I am doing something wrong, if the Unity Ads servers are screwing up, or something else.

Here is the script being used for playing the ad, in it's entirety:

           using System.Collections;
           using System.Collections.Generic;
           using UnityEngine;
           using UnityEngine.Advertisements;
           using UnityEngine.SceneManagement;

           public class PlayAd : MonoBehaviour
      {
           public static PlayAd Instance { set; get; }
 public void Start()
 {
     Instance = this;
     DontDestroyOnLoad(gameObject);
     Advertisement.Initialize("1370212", true);
     var showOptions = new ShowOptions
     {
         resultCallback = HandleShowResult
     };
     if (Advertisement.IsReady())
     {
         Advertisement.Show();
     }




 }
 public string LoadLevel
 {
     get
     {
         return loadLevel;
     }

     set
     {
         loadLevel = value;
     }
 }

 [SerializeField] private string loadLevel;
 private void HandleShowResult(ShowResult result)
 {
     switch (result)
     {
         case ShowResult.Finished:

             LevelLoad();
             break;

         case ShowResult.Skipped:
             break;

         case ShowResult.Failed:

             break;
     }
 }
 public void LevelLoad()
 {
     SceneManager.LoadScene(LoadLevel);






 }

 
 }
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aditya · Apr 04, 2017 at 05:31 AM 0
Share

try Advertisement.isSupported to check if unity ads is supported on your device or not and initialize ads only if they are not initialized with this Advertisement.isInitialized

avatar image Joe-Censored · Apr 04, 2017 at 04:59 PM 0
Share

What platform are you building for?

avatar image dhar174 · Apr 05, 2017 at 04:45 PM 0
Share

@Joe-Censored I am building for Android, Google VR specifically. I am new to C# and in fact Unity itself, of course up til now I have found Unity to have a nice easy learning curve and I am able to accomplish much as far as actual game-building. However, it's just these darn ads I cant seem to get to work, and I am wondering if the problem is on Unity Ads end.

avatar image dhar174 · Apr 05, 2017 at 05:07 PM 0
Share

@aditya I have updated my script with the functions you suggested. As of yesterday, the Test Ads stopped working for some reason, and when they did work it was a crapshoot. Now the test ad comes up every time- not sure if real ads work yet, but I'm liking the way this script works. I am new to scripting, if no-one can tell. But I learn fast.

Now my problem (assu$$anonymous$$g real ads are working, have to test) is to use Scenemanagement.LoadScene to load my next scene, and I am unsure where to put loadscene function (in my case it is a public void called LevelLoad, not to be confused with my public string LoadLevel which merely tells which scene to load).

Whereabouts should I put my void to load the next scene within the new script? I am pasting my newly written script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Advertisements; using UnityEngine.Scene$$anonymous$$anagement;

public class PlayAd : $$anonymous$$onoBehaviour { public static PlayAd Instance { set; get; } public void Start() { Instance = this; DontDestroyOnLoad(gameObject); if(Advertisement.isSupported) { Advertisement.Initialize("1370212", true); // ...initialize. }

     var showOptions = new ShowOptions
     {
         resultCallback = HandleShowResult
     };

    
     
 }

 IEnumerator check ()
 {
     while (!Advertisement.isInitialized || !Advertisement.IsReady())
     {
         yield return new WaitForSeconds(0.5f);
     }
     if (Advertisement.IsReady())
     {
         Advertisement.Show();
         LevelLoad();
     }
    
 }

 private void Update()
 {
     StartCoroutine(check());
 }

 public string LoadLevel
 {
     get
     {
         return loadLevel;
     }

     set
     {
         loadLevel = value;
     }
 }

 [SerializeField] private string loadLevel;
 private void HandleShowResult(ShowResult result)
 {
     switch (result)
     {
         case ShowResult.Finished:

             LevelLoad();
             break;

         case ShowResult.Skipped:
             break;

         case ShowResult.Failed:

             break;
     }
 }
 public void LevelLoad()
 {
     Scene$$anonymous$$anager.LoadScene(loadLevel);






 }

 

}

avatar image dhar174 · Apr 05, 2017 at 07:21 PM 0
Share

@aditya O$$anonymous$$G something worked. Test ads on my android test device are now playing. THAN$$anonymous$$ YOU SO $$anonymous$$UCH!

The level is loading as well, however the problem I'm having is the ad keeps playing repeatedly, but I'm sure it's a simple fix...would it involve putting another "if" statement under the IEnumerator? I'm going to check the documentation, I'm not sure quite how to go about this.....I apologize for my obvious amateurism, this is my first published game and I'm self-learning. Any pointers for preventing the ads from repeating? I know, amateur hour.... I got this far without help, to be fair :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by dhar174 · Apr 06, 2017 at 10:10 PM

The answer was apparently that the ad was trying to load before it was initialized. As @aditya suggested, I used Advertisement.isSupported first, then, importantly, Advertisement.isInitialized. This caused the ads to play continuously, but all I had to do was make sure they were in the right place as well as StartCoroutine and StopCoroutine. Thanks for the help all! Problem solved, and I'm feeling a lot more comfortable with C# scripting.

For anyone else confused by this issue, the code I ended up finishing with is below:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Advertisements;
 using UnityEngine.SceneManagement;

 public class PlayAd : MonoBehaviour
 {
 public static PlayAd Instance { set; get; }
 public void Start()
 {
     Instance = this;
     DontDestroyOnLoad(gameObject);
     if(Advertisement.isSupported)
     { 
         Advertisement.Initialize("1370212", true); // ...initialize.
     }
     StartCoroutine("Check");




 }

 IEnumerator Check ()
 {
     while (!Advertisement.isInitialized || !Advertisement.IsReady())
     {
         yield return new WaitForSeconds(0.5f);
     }
     if (Advertisement.IsReady())
     {
         var showOptions = new ShowOptions
         {
             resultCallback = HandleShowResult
         };
         Advertisement.Show(showOptions);
         StopCoroutine("Check");
     }   LevelLoad();
     
     
 }

   

 public string LoadLevel
 {
     get
     {
         return loadLevel;
     }

     set
     {
         loadLevel = value;
     }
 }

 [SerializeField] private string loadLevel;
 private void HandleShowResult(ShowResult result)
 {
     switch (result)
     {
         case ShowResult.Finished:
             StopCoroutine("Check");
             LevelLoad();
             break;

         case ShowResult.Skipped:
             StopCoroutine("Check");
             LevelLoad();
             break;

         case ShowResult.Failed:
             StopCoroutine("Check");
             LevelLoad();
             break;
     }
 }
 public void LevelLoad()
 {
     SceneManager.LoadScene(loadLevel);






 }

 

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

variable not being changed outside of a method. [C#] 3 Answers

Look On Cursor Problem 1 Answer

Setting a random animation frame from multiple animations 0 Answers

C# Children of GameObject has it's script disabled after SetActive(false) and SetActive(true) 1 Answer

My raycast wont function properly 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges