Ch. 5 – Singleton not found by compiler

Home Forums Books Mastering Unity 2D Game Development Ch. 5 – Singleton not found by compiler

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #9662
    Adam Roberts

    Hi Simon!

    Tackling the messaging manager but I’ve hit a roadblock with the Singleton pattern. Using Unity 5.3.4 if it matters.

    In “MessagingManager.cs” I used the book’s declaration first…

    public class MessagingManager : MonoBehaviour {...

    But I got a bunch of null reference exceptions.

    Then I came to the forum and tried:

    public class MessagingManager : Singleton {

    and later…

    public class MessagingManager : Singleton<MessagingManager> {

    which I lifted from the chapter 6 files.

    With both of the Singleton uses, the compiler keeps telling me I’m “missing an using directive or assembly reference.” Which I assume means my “using” headers don’t contain a singleton definition. To support that theory, MonoDevelop has the Singleton keyword highlighted in red.

    This is my header for MessagingManager currently:

    which is in every version…book, forum, sample code…


    Any ideas why my Unity doesn’t like the Singleton key word?


    OK, so the title walks through a few different patterns for implementing a singleton when the system needs to ensure there is only ever one instance of the manager.

    The first (on page 145) uses the following static property (and surrounding code) to be used to access the class instead of accessing it directly:

    public static MessagingManager Instance { get; private set; }

    Thus all code wanting to talk to the Messaging Manager all do so through “MessagingManager.Instance”

    However this can be very laborious over time having to keep adding the same boilerplate code to every manager, so the second method (shown on page 171), extracts all this boilerplate and put’s it in a separate class (Called Singleton) and then simply lets you implement it by using this new class as a base instead of typing/copying it all again.

    This is demonstrated on page 171 for the conversation manager:

    public class ConversationManager : Singleton<ConversationManager>

    At the end of Chapter 5 it challenges you to implement this elsewhere, such as the MessagingManager.

    So in answer to your question, you need the “Singleton” C# script from the Assets\Scripts\Classes folder which provides the Singleton<> interface.


    Hope this helps

    Simon (Darkside) Jackson

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