游戏对象已被破坏,但您仍在尝试访问它

发布于 2025-01-16 06:56:15 字数 2321 浏览 7 评论 0原文

我正在尝试制作一个统一的广告,以便当玩家输了时,他可以观看广告继续。但是,当我更改场景并尝试再次观看广告时,它显示 HeartMenu.SetActive(false) 的游戏对象已被销毁,但您仍在尝试访问它,但我的脚本只是将心形菜单设置为 SetActive(false) )。我该如何解决这个问题?

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);
    }
}

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文