Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by tharinduwika · Jan 20, 2021 at 11:57 AM · unity adsasyncwaitasynchronous

Identify unity advertisement is finished from another script?

When I'm trying to get to know whether my advertisement is finished or not, It doesn't work properly. In the Level1 script, it directly runs without waiting.

I have several levels let say level 1,2 3 and one AD display script.

Sample Level1 Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 
 public class Level1 : MonoBehaviour
 {
     public GameObject slot1;
     public GameObject slot2;
     public GameObject slot3;
 
     public GameObject Chemical1;
     public GameObject Chemical2;
     public GameObject Chemical3;
 
     public int[] moves = new int[2];
     private static int moveCount =0;
     public static int starCount =0;
     private static bool answer;
 
     ScoreManager scoreManager = ScoreManager.Instance();
     // Start is called before the first frame update
     void Start()
     {
         
         moves[0] = 3;
         moves[1] = 5;        
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     public void onCheckButtonClick()
     {
         moveCount = scoreManager.getMoveCount();
         scoreManager.setMoveCount();
         int currentLevel = SceneManager.GetActiveScene().buildIndex -1;
 
         if ((slot1.transform.position == Chemical1.transform.position) &&
             (slot2.transform.position == Chemical2.transform.position) &&
             (slot3.transform.position == Chemical3.transform.position)
             )
         {
             Debug.Log("Hell yeahh, you correct mann......!");
             if(moveCount <= moves[0])
             {
                 starCount = 3;
             }
             else if (moveCount <= moves[1])
             {
                 starCount = 2;
             }
             else
             {
                 starCount = 1;
             }
 
             PlayerPrefs.SetInt("starCount", starCount);
             PlayerPrefs.SetInt("currentLevel", currentLevel);
             PlayerPrefs.SetInt("answer", 1);
 
             SceneManager.LoadScene(1);
 
         }
         else
         {
             PlayerPrefs.SetInt("starCount", 0);
             PlayerPrefs.SetInt("currentLevel", currentLevel);
             PlayerPrefs.SetInt("answer", 0);
 
             SceneManager.LoadScene(1);
         }
         
     }
 
     public void onHomeButtonClick()
     {
         SceneManager.LoadScene(0);
     }
 
     public void onHintButtonClicked()
     {
         //ask dialog box
         //implement advertiestment here
         FindObjectOfType<ShowAd>().DisplayReweredVideo();
         if (FindObjectOfType<ShowAd>().rewardPlayer())
         {
             giveHint();
         }
         Debug.Log("Hint button clicked");
     }
 
     public void giveHint()
     {
         Chemical1.transform.position = Vector3.Lerp(Chemical1.transform.position, slot1.transform.position, 1);
     }
 }
 



Show AD script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Advertisements;
 
 public class ShowAd : MonoBehaviour, IUnityAdsListener
 {
     // Start is called before the first frame update
 
     private string googlePlayID = "3980107";
     bool gameMode = true;
     string myPlacementId = "rewardedVideo";
 
     private bool eligibleForReward;
     void Start()
     {
         Advertisement.AddListener(this);
         Advertisement.Initialize(googlePlayID, gameMode);
     }
 
     public bool rewardPlayer()
     {
         return eligibleForReward;
     }
 
     // Update is called once per frame
     public void DisplayReweredVideo()
     {
         Advertisement.Show(myPlacementId);
     }
 
     // Implement IUnityAdsListener interface methods:
     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
     {
         // Define conditional logic for each ad completion status:
         if (showResult == ShowResult.Finished)
         {
             eligibleForReward = true;
             // Reward the user for watching the ad to completion.
         }
         else if (showResult == ShowResult.Skipped)
         {
             eligibleForReward = false;
             // Do not reward the user for skipping the ad.
         }
         else if (showResult == ShowResult.Failed)
         {
             Debug.LogWarning("The ad did not finish due to an error.");
         }
     }
 
     public void OnUnityAdsReady(string placementId)
     {
         // If the ready Placement is rewarded, show the ad:
         if (placementId == myPlacementId)
         {
             // Optional actions to take when the placement becomes ready(For example, enable the rewarded ads button)
         }
     }
 
     public void OnUnityAdsDidError(string message)
     {
         // Log the error.
     }
 
     public void OnUnityAdsDidStart(string placementId)
     {
         // Optional actions to take when the end-users triggers an ad.
     }
 
     // When the object that subscribes to ad events is destroyed, remove the listener:
     public void OnDestroy()
     {
         Advertisement.RemoveListener(this);
     }
 }
 



What is the proper way to trigger the giveHint() function in Level1 script after playing advertisement?

(Please note that there are several scripts like level1)

Can anyone guide me to solve this issue? Thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tharinduwika · Jan 20, 2021 at 01:59 PM

I found my own solution. Correct me If I'm wrong.

In Level1 Script.. public void onHintButtonClicked() { //ask dialog box //implement advertiestment here FindObjectOfType().DisplayReweredVideo(); StartCoroutine(giveHint()); }

 IEnumerator giveHint()
 {
     while (!FindObjectOfType<ShowAd>().rewardPlayer())
     {
         yield return new WaitForSeconds(0.5f);
     }
     Chemical1.transform.position = Vector3.Lerp(Chemical1.transform.position, slot1.transform.position, 1);
 }

In showAd script, I declared a boolean and made it private. Then I let it change from false to true after completing the ad. I call that variable to Level1 script by calling get function.

That function is,

   public bool rewardPlayer()
     {
         return eligibleForReward;
     }

Thank you everyone. Let me know I'm doing anything wrong.

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.

Follow this Question

Answers Answers and Comments

113 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

Related Questions

Asyncchronous image loading 2 Answers

Asynchronous socket hangs intermittently on iOS 1 Answer

Async/Await function hangs when caller has infinite loop 1 Answer

How can I asynchronously load and save text files? 1 Answer

Making an interface for web requests 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