0
\$\begingroup\$

I'm trying to add unity ads to my game. In my game there are three panels - startgame, ingame and gameover. Once the game starts, only the startgame panel is active and once you tap on the screen, the ingame panel gets actived and startgame panel is deactivated. In the ingame panel I have added a button to show ads (once an ad is ready) so that after watching the ad, user can receive 100 additional points. Here is my admanager script:

string GooglePlay_ID = "123456";
bool test_mode = true;
string myPlacementId = "rewardedVideo";    

public Button inGameAdBtn;    

void Start()
{       
  inGameAdBtn = GetComponent<Button>();
  inGameAdBtn.interactable = Advertisement.IsReady(myPlacementId);
  if (inGameAdBtn) inGameAdBtn.onClick.AddListener(ShowRewardedVideo);
  
  Advertisement.AddListener(this);        
  Advertisement.Initialize(GooglePlay_ID, test_mode);
}

public void ShowRewardedVideo()
{        
    Advertisement.Show(myPlacementId);
}

public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{

    if (showResult == ShowResult.Finished)
    {
        GameManager.instance.GetPoints();      
    }

else if (showResult == ShowResult.Skipped)
    {

    }

    else if (showResult == ShowResult.Failed)
    {
        Debug.LogWarning("The ad did not finish due to an error.");
    }
}

public void OnUnityAdsReady(string placementId)
{
    // If the ready Placement is rewarded, show the ad:
    if (placementId == myPlacementId)
    {
        inGameAdBtn.interactable = true;
    }
}

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

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

When I hit play, I get an error in the console saying - NullReferenceException: Object reference not set to an instance of an object. When I click on it, it takes to this line of code "inGameAdBtn.interactable = Advertisement.IsReady(myPlacementId);".

I believe this is because once the game starts, the ingame panel is inactive and the admanager script is looking for the inGameAdBtn (which is in the ingame panel). Is there any way to resolve this issue?

EDIT

I have added a screenshot of the game. This is after I hit play, the first panel that is activated is the StartPanel and once I tap on the screen, the next panel will activate (InGamePanel) and the StartPanel will deactivate. The inGameAds button is in the InGamePanel which is currently inactive.

GameInspector

\$\endgroup\$
2
  • \$\begingroup\$ Can you show us the inspector for the object this script is on, so we can see the button component attached to it? \$\endgroup\$ – DMGregory Oct 18 '20 at 13:50
  • \$\begingroup\$ @DMGregory , I have added a screenshot. \$\endgroup\$ – single arrow games Oct 19 '20 at 4:31
0
\$\begingroup\$
inGameAdBtn = GetComponent<Button>();

This line of code says "throw away whatever the designer assigned to the inGameAdBtn in the Inspector, and replace it with the Button component attached to this object.

...but there is no Button component attached to this object in your screenshot. So this throws away a perfectly good button and replaces it with null, the absence of a button.

Naturally when you next try to use this button, while holding only the absence of a button, you get an exception.

If you plan to wire up this reference in the Inspector, delete this line of code that tries to search for it on this object — it's not doing anything you want it to do.

\$\endgroup\$
1
  • \$\begingroup\$ thanks a lot for the help...the error is not coming up any more... \$\endgroup\$ – single arrow games Oct 19 '20 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.