Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by akhilomar555 · May 06, 2020 at 10:56 AM · unity adsstartcoroutinerestart game

Object is destroyed but still trying to access Error. I am trying to implement restart Button but got the error.

I had implemented unity ads with adButton. Now I function it in a way that after using adButton you can't use it more and you have to restart the game and after restarting your game will restart with basic conditions. But after restart then adButton should also available for one time use as by the condtion. But as I use that adButton the game will not resume by StartCoroutine(StartGame(1.0f)). It log the following error : MissingReferenceException: The object of type 'App_Initialize' 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.

App_Initialize.cs

 void Awake(){
     Shader.SetGlobalFloat("_Curvature", 2.0f);
     Shader.SetGlobalFloat("_Trimming", 0.1f);
     Application.targetFrameRate = 60;
 }
 // Start is called before the first frame update
 void Start()
 {       
     player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
     inMenuUI.gameObject.SetActive(true);
     inGameUI.gameObject.SetActive(false);
     gameOverUI.gameObject.SetActive(false);
 }

 public void PlayButton(){
 if (hasGameStarted == true){
     StartCoroutine(StartGame(1.0f));
 }
 else{
      StartCoroutine(StartGame(0.0f));
 }
 }
 public void PauseButton(){
     player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
     hasGameStarted = true;
     inMenuUI.gameObject.SetActive(true);
     inGameUI.gameObject.SetActive(false);
     gameOverUI.gameObject.SetActive(false);
 }
 public void GameOver(){

     player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
     hasGameStarted = true;
     inMenuUI.gameObject.SetActive(false);
     inGameUI.gameObject.SetActive(false);
     gameOverUI.gameObject.SetActive(true);   
     if (hasSeen == true && restartButton!=null){
         adButton.GetComponent<Image>().color = new Color(1, 1, 1, 0.5f);
         adButton.GetComponent<Button>().enabled = false;
         adButton.GetComponent<Animator>().enabled = false;
         restartButton.GetComponent<Animator>().enabled = true;

     }
 }    
 
 public void RestartGame(){
   
     SceneManager.LoadScene(0); //Start scene from index inside LoadScene(0)
     // Application.LoadLevel(Application.loadedLevel);
 }
 public void RewardSeen(){
     hasSeen = true;
     StartCoroutine(StartGame(1.0f));
 }

 IEnumerator StartGame(float waitTime){
     inMenuUI.gameObject.SetActive(false);
     inGameUI.gameObject.SetActive(true);
     gameOverUI.gameObject.SetActive(false);
     yield return new WaitForSeconds(waitTime);
     player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
     player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY;
 }

}

RewardedAdsButton.cs

 #if UNITY_IOS
 private string gameId = "placeholder";
 #elif UNITY_ANDROID
 private string gameId = "placeholder";
 #endif

 Button myButton;
 public string myPlacementId = "rewardedVideo";
 

 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) {
         // StartCoroutine(StartGame(1.0f));
         seeenAd.RewardSeen();
     } else if (showResult == ShowResult.Skipped) {
         // Do not reward the user for skipping the ad.
     } else if (showResult == ShowResult.Failed) {
         Debug.LogWarning ("The ad did not finish due to an error.");
     }
 }

 public void OnUnityAdsDidError (string message) {
     // Log the error.
 }

 public void OnUnityAdsDidStart (string placementId) {
     // Optional actions to take when the end-users triggers an ad.
 } 

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Blackview · Jul 06, 2021 at 09:29 PM

Seeing as no one is bothering to answer this i'll start it off, from my understanding the issue for this is that the Ads listener gets duplicated. A way to partially fix this seems to remove the listener when the player is rewarded.

You can do this by adding this line inside your "OnUnityAdsDidFinish" public void.

Advertisement.RemoveListener(this);

However! I still seem to encounter this issue if i restart the game straight away before watching an ad, so i'm still trying to figure that one out.

Comment
Add comment · Share
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

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

127 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

Related Questions

Brain exploding ... Problem with an internal Coroutine manager 3 Answers

IEnumerator stops at yield 1 Answer

Custom fixed update cycle? 6 Answers

Using WaitForSeconds and trigger 1 Answer

Can I active some static function in ScriptableObject? 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