Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6736727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:09:51+00:00 2026-05-26T11:09:51+00:00

I decided to implement a simple messaging system into my project. I’m using this

  • 0

I decided to implement a simple messaging system into my project. I’m using this one: CSharpMessenger Extended (it’s implemented with static methods).

It’s very strange that when I call a method directly everything is working correctly. But when I broadcast a message with the messaging system I get a NullReferenceException on some of the game objects. To my surprise adding the line if (_gameObject == null) return; solves the problem. However it’s not an option to add a check if the object is null to every place where I get this exception.

What might be the problem?

Here’s my code for broadcasting a message:

public class Head : MonoBehaviour {

    public Snake snake;

    void OnControllerColliderHit (ControllerColliderHit hit)
    {
        if ( hit.gameObject.GetComponent<Terrain>() )
            return;

            //This way everything was working without any surprises.
            //snake.PropCollected( hit.gameObject.GetComponent<Prop>() );
            //Using messaging system instead
        if ( hit.gameObject.GetComponent<Prop>() )
            Messenger<Prop>.Broadcast( "prop collected", hit.gameObject.GetComponent<Prop>() );
            Destroy(hit.gameObject);

        }
    }

Here’s how I subscribe to the event and respond to it:

public class Snake : MonoBehaviour {

    public GameObject headBlock;

    public GameObject snakeBlocks;

    int lastSnakeBlockId;

    private GameObject FindBlockWithId( int _id )
    {
            if (!snakeBlocks) return null;    //This line somehow solves the problem; However the object is 100% assigned in the editor.

            foreach (Transform child in snakeBlocks.transform) {
                if (child.gameObject.GetComponent<SnakeBlockScript>().blockId == _id)
                {
                    return child.gameObject;
                }
            }

        return headBlock;
    }

    void Awake()
    {
        //Set up message listeners
        Messenger<Prop>.AddListener("prop collected", AddBlock);    
    }

    public void AddBlock(Prop _prop)
    {
        GameObject lastBlock = FindBlockWithId(lastSnakeBlockId - 1);
        if (!lastBlock) return;

        //Some more code
        //...
    }
}

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:09:52+00:00Added an answer on May 26, 2026 at 11:09 am

    It sounds like you have a condition where “snakeBlocks” is re-initialized somewhere else in the application, and this thread happens to hit occasionally during that reference being set to something else.

    Something Like:

    snakeBlocks = null;
    
    //Do some stuff.
    
    snakeBlocks = someNewInstanceOfGameObject;
    

    Just a thought.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in the midst of a university project and have decided to implement a
I decided to make a system for a client using Castle ActiveRecord , everything
I decided to implement a very simple program recursively, to see how well Java
I want to implement a hashmap into my code, so I decided to stick
I decided to implement OpenID login into my C++ server. I looked at list
I'm working on a simple plugin system, where third party plugins implement a Plugin
I'm trying to implement a system in MySQL to store hierarchical data. I've decided
I'm starting to learn C++ and as an exercise decide to implement a simple
I'm a beginner (self-learning) programmer learning C++, and recently I decided to implement a
I'm still trying to decide whether my (home) project should use UTF-8 strings (implemented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.