Ch5, MessagingClientReceiver

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

This topic contains 15 replies, has 2 voices, and was last updated by  Simon (darkside) Jackson 2 years, 1 month ago.

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

    Hunor Barabas

    Hello!

     

    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!

    #7082

    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:
    “MessagingManager.Instance.Subscribe(ThePlayerIsTryingToLeave);”

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

    #7144

    Hunor Barabas

    That solved all my problems. Thank you!

    #7160

    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 : http://pastebin.com/nQLapncp

    MessagingClientReceiver Script : http://pastebin.com/hMxikRsf

    #7165

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

    #7176

    Nguyen Viet Hoang

    That fix all my problem, thank you!

    #7182

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

    Simon (Darkside) Jackson
    @SimonDarksideJ

    #9991

    Leonardo

    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

    http://imgur.com/FFoQ2dt

     

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

    #9992

    Leonardo

    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

    #9993

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

    #9994

    Leonardo

    WoW thx allot for attention and is over-here the link
    https://www.dropbox.com/s/1r3lt3c7g5jtilz/Assets.rar?dl=0

    #10023

    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
    Destroy(gameObject);
    }

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

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

     

    Hope this helps and good luck on your gamedev journey

    Simon (Darkside) Jackson
    @SimonDarksideJ

    #10026

    Leonardo

    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

     

    http://imgur.com/e2etZ2h

    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

    #10028

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

    #10033

    Leonardo

    UUUUUUUUUUUUUUAAAAAAAAAAAAAAAAAAAAAAAAA

    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.