- Home /
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023639im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating a fade out and in transition for loading. Functions not executing in order. 1 Answer
Async parse.com query not working on Android/iOS 2 Answers
Instantiate fails with no errors 0 Answers
Take screenshot and use it as texture using ScreenCapture 2 Answers
AsyncOperation activating immediately even with async.allowSceneActivation = false; 0 Answers