Chapter 11-12 Code issue

Home Forums Books Mastering Unity 2D Game Development Chapter 11-12 Code issue

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • #5071

    I get “NullReferenceException: Object reference not set to an instance of an object” error message when I click New Game from MainMenu scene.


    Curious. At what point did this occur and was the first time you saw it when you added the Menu code in Ch 11?

    The error line it is referring to is when it is trying to subscribe to the Messaging Manager on line 7 of the MessagingClientReceiver.cs script.


    The possible reason for this could be if the  MessagingManager hadn’t been updated to a Singleton class.

    Can you confirm if you have updated the beginning of your MessagingManager script as follows:

    public class MessagingManager : Singleton<MessagingManager>


    protected MessagingManager() { } // guarantee this will be always a singleton only – can’t use the constructor!


    Simon (Darkside) Jackson


    I already know what is the problem. I found (singleton) MessagingManager game object at hierarchy of the <span style=”font-family: ‘Open Sans’, sans-serif; font-size: 12px; line-height: 24.2999992370605px;”>MainMenu scene. After I click New Game,  OnDestroy function will be called and applicationIsQuitting variable in Singleton class will set to true. Then, the </span>(singleton) MessagingManager game object will be destroyed. So, when switch to new scene, as applicationIsQuitting variable is still remain true, it will still keep returning null and that’s why new (singleton) MessagingManager game object will never create again.




    OK, going through this now and it is very odd.
    Just put a load of logging data in and all the Singletons are being destroyed when loading The Main scene.

    But The messaging manager isn’t even crated yet which is just odd. (actually it can’t be created because the FadInOutManager destroy has been called, which sets the “applicationIsQuitting” property, which in turn stops new Singletons from being created because Unity is closing)

    Also for some reason OnDestroy is being called even though the FadeInOutManager is using “DontDestroyOnLoad”, which means it should only be called when it is actually destroyed when Unity closes / quits.

    Hmm, going to investigate further as this smells like a new Unity bug.

    Simon (Darkside) Jackson


    Right, found the issue.

    In the sample code an instance of “MessagingManager” has been added to the “MainMenu” scene by default somehow.

    As the type of singletons used in the book (programatic singletons), they are not meant to be added this way, so that is what is causing the error. Because it wasn’t created by Singleton, the DontDestroyOnLoad isn’t set, so when the GO is destroyed, it thinks the app is quitting.

    To fix, just delete the “MessagingManager” from the “MainMenu” scene and it will run just fine.


    Unsure how that got into the code download and can only apologise for the confusion there.

    Simon (Darkside) Jackson

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Mastering Unity 2D Game Development’ is closed to new topics and replies.