Reply To: Ch. 5 – Singleton not found by compiler

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

#9666

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
@SimonDarksideJ