Ch5, MessagingClientReceiver

Home Forums Books Mastering Unity 2D Game Development Ch5, MessagingClientReceiver

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
  • #7059
    Hunor Barabas



    I have a problem that comes up quite often, but this time I am not sure how to solve it. I got the following error a few times while making the game: ” Null reference exception. Object reference is not set to an instance of an object”. Somehow I managed to fix the problem every time, except now.


    I am at the point where I should attach the MessagingClientReceiver to Greybeard, but this error pops up:

    NullReferenceException: Object reference not set to an instance of an object

    MessagingClientReceiver.Start () (at Assets/Assets/Scripts/MessagingClientReceiver.cs:7)


    I am using Unity 5. Hope you can help me with this matter. Thank you in advance!


    Right, sorry to hear you are having troubles, let’s see what we can do.

    Now I note that the error is stating place in the Start method of the MessagingClientReceiver script, the only thing in the start method is the following:

    So the only possible reason I can think this would cause an error was if the MessagingManager script you are using is not implementing the Singleton pattern.

    You might want to check the beginning of the MessagingManager script and ensure it looks as follows:

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

    Note, the use of the Singleton as the base of the script which uses the generic singleton behaviour I describe in Chapter 5 with the ConversationManager.
    You can also see this implemented in the downloadable code in chapter 6 (as the chapter suggests you implement the same behaviour for the messaging manager.

    Alternatively, you can keep the MessagingManager as it is, but ensure that the STATIC property on the script it there called Instance, as shown below:

    // Static singleton property
    public static MessagingManager Instance { get; private set; }

    But I’d recommend the first approach.

    Hope this helps.

    Simon (Darkside) Jackson

    Hunor Barabas

    That solved all my problems. Thank you!

    Nguyen Viet Hoang

    Hello Simon,

    I have the same problem with ‘<span style=”font-family: ‘Open Sans’, sans-serif; font-size: 12px; line-height: 24.2999992370605px; text-align: center;”>Hunor Barabas’ and i’m trying to fix it like your post, but it still have that problem. Can you tell me why? Thank you.</span>

    MessagingManager Script :

    MessagingClientReceiver Script :


    Quick Fix @Nguyen you have implemented BOTH the Singleton<MessagingManager> and an INSTANCE property.  You can only use one of the methods.


    If you check the script in the download code sample in Ch6, you should see the full messaging Manager singleton implementation.

    Basically, you have a property called “Instance” in your script that is overriding the Instance property in the Singleton base class.  This will likely cause a conflict and mean the class isn’t initialised property.


    Hope this helps.

    Simon (Darkside) Jackson

    Nguyen Viet Hoang

    That fix all my problem, thank you!


    Glad to hear it @Nguyen, good luck on your RPG adventure!

    Simon (Darkside) Jackson


    Hello Simon, I read on your book, it’s amazing book, but in chapter 5 on the same error I have the same problem of Hunor Barabas, I make the changes on <span style=”font-family: ‘Open Sans’, sans-serif; font-size: 12px; line-height: 24.3px;”>MessagingManager: MonoBehaviour to Singleton</span>

    first the Visual Studio say Severity Error

    please open link to see the error photo


    but now I dont know what I need reference with the Singleton please help me


    hm and the NullReferenceException: Object reference not set to an instance of an object

    MessagingClientReceiver.Start () (at Assets/Scripts/Messaging/MessagingClientReceiver.cs:7)

    dont solve


    Hi @leonardo  Thank you for supporting my works and choosing my book to help you on your journey.

    Could you zip up your Assets folder for your project, upload it to dropbox or other file share and send me the link and I’ll help get you moving.

    Simon (Darkside) Jackson


    WoW thx allot for attention and is over-here the link


    Sorry it took so long to get back to you @leonardo, work stuff is pretty annoying at times.


    I’ve downloaded your project and found 2 places you are having issue with the Messaging manager.

    First, the class you are passing in to the singleton is wrong, for the Messaging Manager you should be using a singleton of the messaging manager.

    So the following:

    public class MessagingManager : Singleton<ConversationComponent>

    Should read:

    public class MessagingManager : Singleton<MessagingManager>

    Also when you implemented the Singleton, you hadn’t removed the old style singleton code first, leaving the “Instance” Property and the code in awake to initialise it.

    So you need to remove the following lines

    public static MessagingManager Instance { get; private set; }

    if (Instance != null && Instance != this)
    // Destroy other instances if it not the same

    //// Save our current singleton instance
    Instance = this;

    // Make sure that the instance is not destroyed between scenes (this is optional)


    Hope this helps and good luck on your gamedev journey

    Simon (Darkside) Jackson


    Hello Mr. Simon, not problem I know is a hard hours of work when you are a development =)


    First thanks to help me =) I understand what have mistake and now when I see on the console log all working fine BUT when I get closer of greybear or closer of the cave portal nothing of conversation pop up here this photo


    I can’t se why the scripts cant see the conversation resource.

    sorry to busing you if you want you can say some tips to me resolve that =P


    thx for attention

    and sorry about annoying


    Don’t worry about it, I enjoy helping people and educating them, hence why I blog so much and wrote these titles 😀


    As to your query.  The “MessagingClientBroadcast” is responsible for sending the “alert” for the conversation to start, which in the book is attached to the Border.  This “shouts” when your player collides with the border.

    Then GreyBeard has the “MessagingClientReceiver”script attached which is listening for when the “MessagingClientBroadcast” shouts.

    The MessagingManager sits in the middle with the “Conversations” list, it listens for the “Shout” and then relays that to whomever is listening, which in this case is GreyBeard.


    If your still struggling, zip me up your assets folder and again and send me the link to help point you in the right direction if you are still struggling.


    Hope that helps.

    Simon (Darkside) Jackson



    thx alot Mr. Simon Darkside ^^

    I have another faut and you can’t see because is on the engine only I forget to attach the broadcast on cave portal loool I need study more OO ^^

    very thankful for help

    and for this powerful book, when I finishing this and practice some much I’ll begin read your another book ^^ (YEAH! I have the two xD)

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