Hi community,
Here’s my problem that i try to deal with since 2 days now (without success …)
How works my game :
1 / scene with loading bar that loads async my menu/lobby scene.
2 / In my menu scene I go to the game Scene by clicking a button : my menu/lobby scene has its own Managers independant from those in the game Scene.
3 / In my game scene i referenced all objects by drag and drop. From here when the player loses I display an overlay panel where i can click to see a rewarded ad video. When the video is done i spin a fortune wheel with banner at the bottom.
Everything works fine in this straight process but when i click a button from the game scene to go back to my menu scene and repeat this process i got this exception :
MissingReferenceException: The object of type 'GameObject' 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.
No more details are given but i noted that before clicking the rewarded ad button all references exists but when i execute the function OnUnityAdsDidFinish my references are null ???
One approach is to execute Debug.Break() right at the same time you load your scene, then single-step the game one frame at a time. Remember scenes load the NEXT FRAME, not right now.
In general, some notes on how to fix a NullReferenceException error in Unity3D
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
In 41 years of programming I’ve never known any other kind.
In this case it is technically a Missing ref exception, which gives you more clues. For one, The error says “this object has been destroyed.”. Now we all know that going from scene to scene will Destroy() gameobjects… so… Hmm! Clues!
What did you find out by single-stepping and inspecting the scene?
I think a forced re initialization of the Advertisement package when loading my game scene should work but i can’t use manual initialization.
I saw other post on it especially this one : Cannot Disable UnityAds Auto Initialization
but i’m not sure where to put this script. What does he mean by “Editor folder” ?