Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by eruizr · Jun 06, 2016 at 06:32 PM · null reference exception

NullReferenceException: Object reference not set to an instance of an object

Hello everyone, I´m new in unity 3d and I have encountered a problem when trying to show an interstitial ad when the game is over.

I find the following error: "NullReferenceException: Object reference not set to an instance of an object GameManager.ShowAd () (at Assets/Scripts/GameManager.cs:114) GameManager.FinJuegoEnemy () (at Assets/Scripts/GameManager.cs:63)"

I tried a lot of things, but I can find the problem. Please , can you help me?

Thank you very much in advance.

Here is the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using GoogleMobileAds.Api;
 
 public class GameManager : MonoBehaviour
 {
     public float startDelay;
     public static GameManager instance = null;
     
     public GameObject canvasIntro;
     public GameObject canvasMenu;
     public GameObject gameBoard;
     public GameObject canvasOverEnd;
     Text textEnd;
 
     InterstitialAd interstitial;
     private BannerView bannerView;
 
     void Awake ()
     {
         //Check if instance already exists
         if (instance == null) {
             //if not, set instance to this
             instance = this;
         }
         //If instance already exists and it's not this:
         else if (instance != this) {
             //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
             Destroy (gameObject);
         }
         //Sets this to not be destroyed when reloading scene
         DontDestroyOnLoad (gameObject);
         canvasIntro.SetActive (true);
         RequestBanner ();
         RequestInterstitial ();
         Invoke ("startMenu", startDelay);
     }
 
     void startMenu ()
     {
         canvasIntro.SetActive (false);
         canvasMenu.SetActive (true);
     }
 
     public void FinJuegoPlayer ()
     {
         gameBoard.SetActive (false);
         canvasOverEnd.SetActive (true);
         textEnd = GameObject.Find ("TextWinLoose").GetComponent<Text> ();
         textEnd.text = "You Win!";
         ShowAd ();
     }
 
     public void FinJuegoEnemy ()
     {
         gameBoard.SetActive (false);
         canvasOverEnd.SetActive (true);
         textEnd = GameObject.Find ("TextWinLoose").GetComponent<Text> ();
         textEnd.text = "Sorry, you lost!";
         ShowAd ();
     }
 
     public void salir ()
     {
         Application.Quit ();
     }
 
     private void RequestBanner ()
     {
         #if UNITY_ANDROID
         string adUnitId = "ca-app-pub-4526259028819910/8305328189";
         #elif UNITY_IPHONE
         string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
         #else
         string adUnitId = "unexpected_platform";
         #endif
 
         // Create a 320x50 banner at the bottom of the screen.
         BannerView bannerView = new BannerView (adUnitId, AdSize.Banner, AdPosition.Bottom);
         // Create an empty ad request.
         AdRequest request = new AdRequest.Builder ().Build ();
         // Load the banner with the request.
         bannerView.LoadAd (request);
     }
 
     private void RequestInterstitial ()
     {
         #if UNITY_ANDROID
         string adUnitId = "ca-app-pub-4526259028819910/6688994189";
         #elif UNITY_IPHONE
         string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
         #else
         string adUnitId = "unexpected_platform";
         #endif
 
         // Initialize an InterstitialAd.
         InterstitialAd interstitial = new InterstitialAd (adUnitId);
         // Create an empty ad request.
         AdRequest request = new AdRequest.Builder ().Build ();
         // Load the interstitial with the request.
         interstitial.LoadAd (request);
     }
 
     public void HideBanner ()
     {
         bannerView.Hide ();
     }
 
     public void ShowAd ()
     {
         if (interstitial.IsLoaded ()) {
             interstitial.Show ();
         }
     }
 }


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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jgodfrey · Jun 06, 2016 at 10:03 PM

Your problem is likely here:

 // Initialize an InterstitialAd.
 InterstitialAd interstitial = new InterstitialAd (adUnitId);

You've already declared a class-level variable named "interstitial" of type InterstitialAd near the top of the posted code. I assume that's the variable you're trying assign an object to in the above code. However, since you redeclare the variable type above, that's a brand new, local (and completely different) variable than the one declared at the top.

Since the global variable was never assigned a value (it was assigned to a different, local variable), it's null when you try to reference it in the ShowAd() method.

To fix it, just drop the type in the above code. So...

 // Initialize an InterstitialAd.
 interstitial = new InterstitialAd (adUnitId);
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
avatar image
0

Answer by eruizr · Jun 07, 2016 at 11:29 AM

Thank you very much. Now it works perfectly.

Comment
Add comment · Show 1 · 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
avatar image naveedkhan358 · Jan 17, 2018 at 07:49 PM 0
Share

NullReferenceException: Object reference not set to an instance of an object chap2.Game$$anonymous$$anager_Toggle$$anonymous$$enu.Toggle$$anonymous$$enu () (at Assets/$$anonymous$$y script/Chapter2/Game$$anonymous$$anager Script/Game$$anonymous$$anager_Toggle$$anonymous$$enu.cs:52) chap2.Game$$anonymous$$anager_Toggle$$anonymous$$enu.Start () (at Assets/$$anonymous$$y script/Chapter2/Game$$anonymous$$anager Script/Game$$anonymous$$anager_Toggle$$anonymous$$enu.cs:15)

There is my game script..down using System.Collections; using System.Collections.Generic; using UnityEngine;

namespace chap2 { public class Game$$anonymous$$anager_TogglePause : $$anonymous$$onoBehaviour {

     private Game$$anonymous$$anager_$$anonymous$$aster game$$anonymous$$anager$$anonymous$$aster;
     private bool isPaused;

     // Use this for initialization
     void OnEnable ()
     {
         SetInitialReferences ();
         game$$anonymous$$anager$$anonymous$$aster.$$anonymous$$enuToggleEvent += TogglePause;
         game$$anonymous$$anager$$anonymous$$aster.InventoryUIToggleEvent += TogglePause;
     }

     // Update is called once per frame
     void OnDisable ()
     {
         game$$anonymous$$anager$$anonymous$$aster.$$anonymous$$enuToggleEvent -= TogglePause;
         game$$anonymous$$anager$$anonymous$$aster.InventoryUIToggleEvent -= TogglePause;
     }

     void SetInitialReferences()
     {
         game$$anonymous$$anager$$anonymous$$aster = GetComponent<Game$$anonymous$$anager_$$anonymous$$aster> ();
     }

     void TogglePause()
     {
         if (isPaused) {
             Time.timeScale = 1;
             isPaused = false;
         } 
         else 
         {

// Time.timeScale = 0; isPaused = true; } } }

}

and the other script is here using System.Collections; using System.Collections.Generic; using UnityEngine;

namespace chap2 { public class Game$$anonymous$$anager_Toggle$$anonymous$$enu : $$anonymous$$onoBehaviour { private Game$$anonymous$$anager_$$anonymous$$aster game$$anonymous$$anager$$anonymous$$aster; public GameObject menu;

     // Use this for initialization
     void Start () 
     {
         Toggle$$anonymous$$enu ();
     }

     // Update is called once per frame
     void Update () 
     {
         CheckFor$$anonymous$$enuToggleRequest ();
     }

     void OnEnabled()
     {
         SetInitialReferences ();
         game$$anonymous$$anager$$anonymous$$aster.GameOverEvent += Toggle$$anonymous$$enu;
     }

     void OnDisabled()
     {
         game$$anonymous$$anager$$anonymous$$aster.GameOverEvent -= Toggle$$anonymous$$enu;
     }

     void SetInitialReferences()
     {
         game$$anonymous$$anager$$anonymous$$aster = GetComponent<Game$$anonymous$$anager_$$anonymous$$aster> ();
     }

     void CheckFor$$anonymous$$enuToggleRequest()
     {
         if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Escape) && !game$$anonymous$$anager$$anonymous$$aster.isGameOver && !game$$anonymous$$anager$$anonymous$$aster.isInventoryUIOn) 
         {
             Toggle$$anonymous$$enu ();
         }
     }

     void Toggle$$anonymous$$enu()
     {
         if (menu != null) {
             menu.SetActive (!menu.activeSelf);
             game$$anonymous$$anager$$anonymous$$aster.is$$anonymous$$enuOn = !game$$anonymous$$anager$$anonymous$$aster.is$$anonymous$$enuOn;
             game$$anonymous$$anager$$anonymous$$aster.CallEvent$$anonymous$$enuToggle ();
         } 
         else 
         {
             Debug.LogWarning("You need to assign a UI GameObject to the Toggle $$anonymous$$enu script in the inspector.");
         }
     }
 }

}

avatar image
0

Answer by kadirkoca · Jul 04, 2019 at 05:30 PM

You request ads before Mobile ads. initialized.

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

60 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

Related Questions

Help with NullReferenceException 0 Answers

Errors appear when i get a bad Raycast hit. Meaning when it hits another object in the scene. 0 Answers

Null object after Instantiation 1 Answer

NullReferenceException... but it was just set...? 1 Answer

Cannot access 2D array element 1 Answer


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