Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by aykut-yilmaz · Dec 19, 2016 at 08:23 AM · unity 5adsyield

How to properly wait for Unity Ad to be watched before continuing game logic?

Hi everyone.

In my game user has a single life. When he dies, I display a menu where he can select to restart the game or watch a Unity ad to continue the game from the point where he died. He can do this only once. If he watches a Unity ad, then continues game and dies again, he is not given the option to watch another one, he has to restart the game. There is a very simple end menu where the user can select to "restart" or "watch ad". If user selects "watch ad", I simply set the boolean wannaWatchAd to true and return to menuScript which is the attached script for the startMenu.

In the very start wannaWatchAd is false, so the first time the script is called, the startMenu is displayed in the else where the Debug.Log with string starting with ">>> 5". When the user dies and selects to watch an ad, wannaWatchAd is set to true and the code goes into blocks marked with Debug.Log entries ">>> 1", then ">>> 2". At that point as I am yielding return from the function ShowRewardedAd(), I expect the code to wait until the co-routine ShowRewardedAd() finishes and continue with the code block marked with ">>> 3" but the code goes right into the block marked with ">>> 4". The result is, user watches the video but can not continue the game and faces the startMenu.

What I see in the debug output is as follows:

 I/Unity   ( 8790): >>> 5 In if WannaWatchAd is false, so we open menu False 0
 I/Unity   ( 8790): >>> 1 Start of MenuScript.Start, WannaWatchAd = True 17.96005
 I/Unity   ( 8790): >>> 2 In if WannaWatchAd is true, so we show ad and yield its return True 17.96005
 I/Unity   ( 8790): >>> 4 Ad is NOT watched, AdWatched = False WannaWatchAd = True 17.96005
 I/Unity   ( 8790): >>> 3 Ad is watched so AdWatched = False WannaWatchAd = False 18.08006

Check the time displayed at the end of lines. Code goes into blocks 2 and 4 but at the same time it is yielding return of the co-routine and once the co-routine ends, it goes to block 3 although AdWatched is false at that time. That is the second thing that puzzles me about how things work in there.

I based my code on the example code snippets given with Unity Ads but I guess they are designed to give the player some reward asynchronously. When I try to actually wait for the ad before continuing with the game logic, they do not work. So, any ideas how I could approach this particular problem? Is it even possible to stop code execution and wait for Unity ad to be watched and then continue?

Thanks in advance.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using UnityEngine.Advertisements;
 
 public class MenuScript : MonoBehaviour {
 
     public GameObject birdie;
 
     public GameObject startMenu;
 
     private IEnumerator Start()
         {
             Debug.Log(">>> 1 Start of MenuScript.Start, WannaWatchAd = " + GameManager.WannaWatchAd + " " + Time.time);
     
             if (GameManager.WannaWatchAd)
             {
                 Debug.Log(">>> 2 In if WannaWatchAd is true, so we show ad and yield its return " + GameManager.WannaWatchAd + " " + Time.time);
     
                 GameManager.AdWatched = false;
     
                 yield return StartCoroutine(ShowRewardedAd());
     
                 if (GameManager.AdWatched)
                 {
                     Debug.Log(">>> 3 Ad is watched so AdWatched = " + GameManager.AdWatched + " WannaWatchAd = " + GameManager.WannaWatchAd + " " + Time.time);
     
                     birdie.GetComponent<BirdController>().ReLive();
                 }
                 else
                 {
                     Debug.Log(">>> 4 Ad is NOT watched, AdWatched = " + GameManager.AdWatched + " WannaWatchAd = " + GameManager.WannaWatchAd + " " + Time.time);
     
                     GameManager.coins = 0;
     
                     startMenu.SetActive(true);
                 }
     
                 GameManager.WannaWatchAd = false;
             }
             else
             {
                 Debug.Log(">>> 5 In if WannaWatchAd is false, so we open menu " + GameManager.WannaWatchAd + " " + Time.time);
     
                 GameManager.coins = 0;
             
                 startMenu.SetActive(true);
             }
     
             yield return null;
         }
     
     
         private IEnumerator ShowRewardedAd()
         {
     #if UNITY_ANDROID || UNITY_IOS
             if (Advertisement.IsReady("rewardedVideo"))
             {
                 var options = new ShowOptions { resultCallback = HandleShowResult };
     
                 Advertisement.Show("rewardedVideo", options);
             }
     #endif        
             yield return null;
         }
     
     
     #if UNITY_ANDROID || UNITY_IOS
         private void HandleShowResult(ShowResult result)
         {
             switch (result)
             {
                 case ShowResult.Finished:
                     GameManager.AdWatched = true;
     
                     Debug.Log("The ad was successfully shown.");
     
                     break;
     
                 case ShowResult.Skipped:
                     Debug.Log("The ad was skipped before reaching the end.");
                     break;
     
                 case ShowResult.Failed:
                     Debug.LogError("The ad failed to be shown.");
                     break;
             }
         }
     #endif
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Advertising 0 Answers

I have a few questions 1 Answer

Unity switches scenes when Ad is closed. 0 Answers

Problem with Unity Ads - UnityAdsEditor: Initialize(1529584, False); UnityEditor.Advertisements.UnityAdsEditor:EditorOnLoad() 0 Answers

Unity Advertisement isn´t existing? 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