I decided to implement a simple messaging system in my project. I use this one: CSharpMessenger Extended (it is implemented with static methods).
It is very strange that when I call the method directly, everything works correctly. But when I pass a message with the messaging system, I get a NullReferenceException on some game objects. To my surprise, adding a line if (_gameObject == null) return; solves the problem. However, this is not an option to add validation if the object is null for every place where I get this exception.
What could be the problem?
Here is my code for broadcasting the message:
public class Head : MonoBehaviour { public Snake snake; void OnControllerColliderHit (ControllerColliderHit hit) { if ( hit.gameObject.GetComponent<Terrain>() ) return;
Here, as 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;
Thanks!
source share