- Home /
Button was destroyed but you are still trying to access it
So I implemented the Unity AD Button for playing a Reward-Video. They have a script for it and I copied it. You only need to implement the Reward the Player gets after watching an AD yourself. That does work but after you die once more, only restart is possible and the Level reloads. After that, when trying to use the AD-Button again, it says the Button was destroyed and cannot be accessed.
Here is the ERROR:
MissingReferenceException: The object of type 'Button' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. RewardedAdsButton.OnUnityAdsDidFinish (System.String placementId, UnityEngine.Advertisements.ShowResult showResult) (at Assets/Scripts/RewardedAdsButton.cs:70)
I tried alot of different things, but even when I make the parent UI-Canvas of the Button not destroy itself on load, it still says the Button was destroyed. I just dont have any ideas anymore. Other Objects dont loose their references after a reload.
Here is the script, for the Reward-Button. Unity markes Line 69 when I click on the ERROR message.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
[RequireComponent (typeof(Button))]
public class RewardedAdsButton : MonoBehaviour, IUnityAdsListener
{
//AndroidID
private string gameId = "NUMBERS";
//AndroidVideoID
private string myPlacementId = "Rewarded_Android";
public Button restartButton;
public GameObject player;
Button myButton;
void Start()
{
myButton = GetComponent<Button>();
// Set interactivity to be dependent on the Placement’s status:
myButton.interactable = Advertisement.IsReady(myPlacementId);
// Map the ShowRewardedVideo function to the button’s click listener:
if (myButton) myButton.onClick.AddListener(ShowRewardedVideo);
// Initialize the Ads listener and service:
Advertisement.AddListener(this);
Advertisement.Initialize(gameId, true);
}
// Implement a function for showing a rewarded video ad:
void ShowRewardedVideo()
{
Advertisement.Show(myPlacementId);
}
// Implement IUnityAdsListener interface methods:
public void OnUnityAdsReady(string placementId)
{
// If the ready Placement is rewarded, activate the button:
if (placementId == myPlacementId)
{
myButton.interactable = true;
}
}
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.
Time.timeScale = 1;
if(restartButton == null)
{
Debug.Log("NO RestartButton found (Reward)");
}
else
{
restartButton.gameObject.SetActive(false);
}
myButton.enabled = false;
myButton.gameObject.SetActive(false);
player.gameObject.SetActive(true);
player.GetComponent<Invulnerability>().noCollision();
}
else if (showResult == ShowResult.Skipped)
{
// Do not reward the user for skipping the ad.
Debug.Log("AD was skipped");
}
else if (showResult == ShowResult.Failed)
{
Debug.LogWarning("Video failed");
}
}
public void OnUnityAdsDidError(string message)
{
// Log the error.
}
public void OnUnityAdsDidStart(string placementId)
{
// Optional actions to take when the end-users triggers an ad.
}
}
Hope someone can help :)
How did you implemented Level Reload? Do you reload the scene? If yes, then which objects are marked as DontDestoryOnLoad?
Your answer
Follow this Question
Related Questions
script trying to access a null gameobject's collider... can't fix 2 Answers
Unity 2018.3 issue! 0 Answers
Unity internal error in Playtest mode. What is the cause and how do I prevent it?? 0 Answers
Error: Unable to load the icon: 'CustomEditorWindow'. (Custom Scene View) 1 Answer
Why wont Unity open? 0 Answers