Hi,
I’m trying to add Unity Level Play to my game. The interstitial ads works perfectly and the rewarded videos play when I want them to. However, sometimes OnAdRewardedEvent() will give me an Null reference exception when I’m trying to call uIManager.Revive(). Other times it works perfectly. Everything that I’m am referencing in the function is assigned in the inspector, but they are all null when I call it in OnAdRewardedEvent(). It doesn’t matter what function I call in the event, everything gives me a null reference exception.
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
public class IronsourceManager : MonoBehaviour
{
#if UNITY_ANDROID
string appKey = "---";
#elif UNITY_IPHONE
string appKey = "---";
#else
string appKey = "unexpected_platform";
#endif
[SerializeField] QuestsManager questsManager;
[SerializeField] DailyRewardManager dailyRewardManager;
[SerializeField] UIManager uIManager;
private const int GamesPerAd = 5;
private int gamesPlayed;
private void Start()
{
IronSource.Agent.validateIntegration();
IronSource.Agent.init(appKey);
gamesPlayed = PlayerPrefs.GetInt("GamesPlayed", 0);
// Check if it's time to show an interstitial ad
if (gamesPlayed >= GamesPerAd)
{
ShowInterstitial();
gamesPlayed = 0; // Reset counter after showing ad
}
else
{
gamesPlayed++;
}
// Save the updated counter
PlayerPrefs.SetInt("GamesPlayed", gamesPlayed);
if (!IronSource.Agent.isInterstitialReady())
{
// If no ad is loaded, load a new interstitial ad
LoadInterstitial();
}
if(!IronSource.Agent.isRewardedVideoAvailable())
{
LoadRewarded();
}
}
private void OnEnable()
{
IronSourceEvents.onSdkInitializationCompletedEvent += SDKInitialized;
//Add AdInfo Interstitial Events
IronSourceInterstitialEvents.onAdReadyEvent += InterstitialOnAdReadyEvent;
IronSourceInterstitialEvents.onAdLoadFailedEvent += InterstitialOnAdLoadFailed;
IronSourceInterstitialEvents.onAdOpenedEvent += InterstitialOnAdOpenedEvent;
IronSourceInterstitialEvents.onAdClickedEvent += InterstitialOnAdClickedEvent;
IronSourceInterstitialEvents.onAdShowSucceededEvent += InterstitialOnAdShowSucceededEvent;
IronSourceInterstitialEvents.onAdShowFailedEvent += InterstitialOnAdShowFailedEvent;
IronSourceInterstitialEvents.onAdClosedEvent += InterstitialOnAdClosedEvent;
//Add AdInfo Rewarded Video Events
IronSourceRewardedVideoEvents.onAdOpenedEvent += RewardedVideoOnAdOpenedEvent;
IronSourceRewardedVideoEvents.onAdClosedEvent += RewardedVideoOnAdClosedEvent;
IronSourceRewardedVideoEvents.onAdAvailableEvent += RewardedVideoOnAdAvailable;
IronSourceRewardedVideoEvents.onAdUnavailableEvent += RewardedVideoOnAdUnavailable;
IronSourceRewardedVideoEvents.onAdShowFailedEvent += RewardedVideoOnAdShowFailedEvent;
IronSourceRewardedVideoEvents.onAdRewardedEvent += RewardedVideoOnAdRewardedEvent;
IronSourceRewardedVideoEvents.onAdClickedEvent += RewardedVideoOnAdClickedEvent;
}
void SDKInitialized()
{
print("SDK is initialized");
}
private void OnApplicationPause(bool pause)
{
IronSource.Agent.onApplicationPause(pause);
}
// Interstitial
#region rewarded
public void LoadRewarded()
{
IronSource.Agent.loadRewardedVideo();
}
public void ShowRewarded(string placement)
{
if (IronSource.Agent.isRewardedVideoAvailable())
{
IronSource.Agent.showRewardedVideo(placement);
}
else
{
Debug.Log("Rewarded ad not available!");
}
}
/************* RewardedVideo AdInfo Delegates *************/
// Other events
// The Rewarded Video ad view is about to be closed. Your activity will regain its focus.
void RewardedVideoOnAdClosedEvent(IronSourceAdInfo adInfo)
{
Debug.Log("Ad close Event");
}
// The user completed to watch the video, and should be rewarded.
// The placement parameter will include the reward data.
// When using server-to-server callbacks, you may ignore this event and wait for the ironSource server callback.
void RewardedVideoOnAdRewardedEvent(IronSourcePlacement placement, IronSourceAdInfo adInfo)
{
Debug.Log("Reward Event");
GrantReward(placement.getPlacementName());
}
// The rewarded video ad was failed to show.
void RewardedVideoOnAdShowFailedEvent(IronSourceError error, IronSourceAdInfo adInfo)
{
}
// Invoked when the video ad was clicked.
// This callback is not supported by all networks, and we recommend using it only if
// it’s supported by all networks you included in your build.
void RewardedVideoOnAdClickedEvent(IronSourcePlacement placement, IronSourceAdInfo adInfo)
{
}
private void GrantReward(string placementName)
{
print("Gives reward to player");
switch (placementName)
{
case "Revive":
uIManager.Revive(); break;
case "Daily_Quests":
questsManager.NewQuests();
PlayerPrefs.SetInt("NewQuestsButtonUsed", 1);
break;
case "Daily_Reward":
dailyRewardManager.GetDailyReward(true); break;
}
}
#endregion
type or paste code here