Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by mattisnaegele02 · Mar 01, 2021 at 12:22 PM · errorunityeditormissingreferenceexception

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 :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pauldarius98 · Mar 01, 2021 at 01:25 PM 0
Share

How did you implemented Level Reload? Do you reload the scene? If yes, then which objects are marked as DontDestoryOnLoad?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

157 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges