1

I am trying to make a unity ad so that when the player loses, he can watch an ad to continue. However, when i change scenes and try to watch the ad again, it shows the gameobject which is the HeartMenu.SetActive(false) has been destroyed but you are still trying to access it but my script just sets the heart menu to SetActive(false). How can i go around this?

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;

public class RewardedAdsButton : MonoBehaviour, 
IUnityAdsLoadListener, IUnityAdsShowListener
{
[SerializeField] Button _showAdButton;
[SerializeField] string _androidAdUnitId = 
"Rewarded_Android";
[SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
string _adUnitId = null; // This will remain null for 
unsupported platforms

public GameObject Restart;

public GameObject HeartMenu;

void Awake()
{
  
#if UNITY_IOS
    _adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
    _adUnitId = _androidAdUnitId;
#endif

  

}

public void Update()
{


 
}

// Load content to the Ad Unit:
public void LoadAd()
{
    // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    Debug.Log("Loading Ad: " + _adUnitId);
    Advertisement.Load(_adUnitId, this);
}

// If the ad successfully loads, add a listener to the button and enable it:
public void OnUnityAdsAdLoaded(string adUnitId)
{
    Debug.Log("Ad Loaded: " + adUnitId);

    if (adUnitId.Equals(_adUnitId))
    {
        // Configure the button to call the ShowAd() method when clicked:
        _showAdButton.onClick.AddListener(ShowAd);
        // Enable the button for users to click:
        _showAdButton.interactable = true;
    }
}

// Implement a method to execute when the user clicks the button:
public void ShowAd()
{
    // Disable the button:
    _showAdButton.interactable = false;
    // Then show the ad:
    Advertisement.Show(_adUnitId, this);
}

// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
    if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
    {
        Debug.Log("Unity Ads Rewarded Ad Completed");
        // Grant a reward.
        HeartMenu.SetActive(false);
        OnDestroy();
        Time.timeScale = 1f;
        // Load another ad:
        Advertisement.Load(_adUnitId, this);
    }
}
Jerry
  • 21
  • 4
  • Please add more details, things like what is `HeartMenu` and how are you accessing it, also, keep in mind that scripts from disabled objects can be accessed, but destroying it makes it impossible. Double check if nothing is destroying your gameobject that contains the `HeartMenu` – Vinícius Almeida Mar 22 '22 at 18:53
  • @ViníciusAlmeida I have edited it to show my full script – Jerry Mar 22 '22 at 19:14
  • Have you tried making it set active again on Awake? –  Mar 22 '22 at 20:01
  • @Mintvbz Yes i have and it didn’t work. The heart menu is meant to become active if the player collides with a missile in the game. This just makes it active and the player can't play the game – Jerry Mar 22 '22 at 23:23
  • Sounds like you are holding onto a reference between scene loads, that referance will beceome invalid as the object will be destroyed by the scene change. Does this object ever outlive a scene change? – Jay Mar 24 '22 at 02:27

0 Answers0