- Home /
When ball hits wall bottom for 15th time , intstead of ad i get error : NullReferenceException: Object reference not set to an instance of an object BallScript.Start () (at Assets/BallScript.cs:44)
using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using UnityEngine.Advertisements;
public class BallScript : MonoBehaviour {
private ManagerAds ads;
public Vector2 startForce;
static int times = 0;
public Text goverText;
public Vector3[] positions;
public Rigidbody2D rb;
void Start()
{
int randomNumber = Random.Range(0, positions.Length);
transform.position = positions[randomNumber];
times = PlayerPrefs.GetInt("Played:", times);
rb.AddForce(startForce, ForceMode2D.Impulse);
ads = GameObject.FindObjectOfType<ManagerAds>();
}
void Update()
{
PlayerPrefs.SetInt("Played:", times);
goverText.text = "Played:" + times.ToString();
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.name == "wall_bottom")
{
times++;
if (times == 15 )
{
Debug.Log("ShowAd");
ads.PlayRewardedAd();
}
SceneManager.LoadScene("wfvv");
}
}
}
Answer by bobisgod234 · Mar 28, 2017 at 02:08 AM
It's telling you your 'ads' variable is null, which suggests that
GameObject.FindObjectOfType<ManagerAds>();
is returning null, which suggests that at the point in time when the Start() function is called, either you have no gameobject in your scene with a ManagerAds component attached, or that object is disabled.
Answer by theGugu · Mar 29, 2017 at 02:48 PM
It seems it can't find the object to work with. ads.PlayRewardedAd() at line 44 can't be executed because ads is probably not set. Make ads (first line) public instead of private and then try to see during execution if it is void or not. Probably the problem is at line 21: ads = GameObject.FindObjectOfType();
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 Answers
I keep getting a null reference exception, can anyone help? 0 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Showing unity ads from other script? 1 Answer
Text field issue switching from scene to another scene 0 Answers