整合统一广告错误在第二次播放



我在项目中添加了Unity广告。在第一次运行时效果很好,这表明广告会显示,但当我第二次点击播放时,它会给我这个错误

MissingReferenceException:类型为"GameObject"的对象已被销毁,但您仍在尝试访问它。脚本应该检查它是否为null,或者不应该销毁对象。

这是我的广告脚本:

public class Monetization : MonoBehaviour, IUnityAdsListener
{
string GooglePlay_ID = "****";
bool GameMode = true;

string myPlacementId = "rewardedVideo";
void Start()
{
Advertisement.AddListener(this);
Advertisement.Initialize(GooglePlay_ID, GameMode);

} 
public void ShowRewardedVideo()
{
// Check if UnityAds ready before calling Show method:
if (Advertisement.IsReady(myPlacementId))
{
Advertisement.Show(myPlacementId);
}
else
{
Debug.Log("Rewarded video is not ready at the moment! Please try again later!");
}
}
// Implement IUnityAdsListener interface methods:
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
// Define conditional logic for each ad completion status:
if (showResult == ShowResult.Finished)
{
// Reward the user for watching the ad to completion.

SceneManager.LoadScene(4);
}

else if (showResult == ShowResult.Skipped)
{
// 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.
}
}

加载新场景时,前一场景中的游戏对象将被销毁。为了防止这种情况,请在货币化脚本中添加以下函数。

void Awake() {
DontDestroyOnLoad(this.gameObject);
}

最新更新