Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Ads on death problem

Discussion in 'Unity Ads & User Acquisition' started by Vasov97, Mar 26, 2017.

  1. Vasov97

    Vasov97

    Joined:
    Aug 4, 2016
    Posts:
    28
    I have script (ManagerScript) where I have PlayRewardedAd void which plays rewarded ad , but when I attached this components to other script nothing happens (when player dies for 15th - by hitting a bottom wall- ad won't play,instead ball just bounces up from wall).So Unity recognizes some action on 15th death ,but not correct action.Here's ballscript :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine.UI;
    4. using UnityEngine.Advertisements;
    5.  
    6. public class BallScript : MonoBehaviour
    7. {
    8.     private ManagerAds ads;
    9.     public Vector2 startForce;
    10.  
    11.     static int times = 0;
    12.     public Text goverText;
    13.     public Vector3[] positions;
    14.     public Rigidbody2D rb;
    15.  
    16.     void Start()
    17.     {
    18.         int randomNumber = Random.Range(0, positions.Length);
    19.         transform.position = positions[randomNumber];
    20.         times = PlayerPrefs.GetInt("Played:", times);
    21.         rb.AddForce(startForce, ForceMode2D.Impulse);
    22.         ads = FindObjectOfType<ManagerAds>();
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         PlayerPrefs.SetInt("Played:", times);
    28.         goverText.text = "Played:" + times.ToString();
    29.     }
    30.  
    31.     void OnCollisionEnter2D(Collision2D other)
    32.     {
    33.         if (other.gameObject.name == "wall_bottom")
    34.         {
    35.             times++;
    36.  
    37.             if (times == 15)
    38.             {
    39.                 ads.PlayRewardedAd();
    40.             }
    41.  
    42.             SceneManager.LoadScene("wfvv");
    43.         }
    44.     }
    45. }
     
    Last edited by a moderator: Mar 26, 2017
  2. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    Last edited: Mar 26, 2017
  3. Vasov97

    Vasov97

    Joined:
    Aug 4, 2016
    Posts:
    28
    NullReferenceException: Object reference not set to an instance of an object
    BallScript.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/BallScript.cs:39)

    I got this error in Editor , when ad should be played
     
  4. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
  5. Vasov97

    Vasov97

    Joined:
    Aug 4, 2016
    Posts:
    28
    Fixed it by attaching ManagerAds script to Ball