Search Unity

GameObject missing in ad manager script Rewarded ads implementation after changing scenes

Discussion in 'Unity Ads & User Acquisition' started by christoszal, Oct 19, 2021.

  1. christoszal

    christoszal

    Joined:
    Jun 6, 2021
    Posts:
    12
    Hello,
    Its the first time i create an ad manager script but im having a weird problem. I have implemented the rewarded ads script that will activate some text and a gameobject when its complete but if i change scene and complete a rewarded ad again then it will give either a nullPointerException or a missingReferenceException telling me that the text or/and the gameobject is missing even though ive added them in the inspector. Does the ad manager script pass through the scenes and the objects get destroyed? I don't understand.
     
  2. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    Hi @christoszal,

    Good question. The short answer is that it depends on how it is implemented. GameObjects are destroyed by default when changing scenes, so if your script is attached to a GameObject, the reference will be destroyed as well. I think what you're probably looking for is DontDestroyOnLoad() - documented here: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

    I hope this helps!
     
    anycolourulike likes this.
  3. christoszal

    christoszal

    Joined:
    Jun 6, 2021
    Posts:
    12
    Hello thanks a lot. I talked to some other people about it too and they actually told me the same thing. The problem is that im no that familiar with DontDestoryOnLoad() and also i had a lot of other objects too that were related to the ads manager script and gameObject(that were not prefabs) and it became kinda messy as this is my first real project. So i implemented this quick but not as good solution. I just added a static bool to the ads manager script that would check if the rewarded ad was completed and then i would do everything else from other scripts.
     
  4. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    Hi @christoszal,

    Happy to hear you have it working now! You'll find that you get better at organizing your projects over time. Try to focus on keeping each of your classes as loosely coupled as possible - this means try to keep your classes separate so that you can change one class without affecting others. Try to make use of common design patterns to help you with this. For instance, I like to implement my ads manager script as a singleton; of course, there are examples where different implementations (or design patterns) make more sense, but singletons typically fit my workflow.

    Keep making games, and you'll find your skills improve dramatically over time!

    I wish you the best of luck!
     
    anycolourulike and christoszal like this.