I have a method that returns a monobehaviour from a dictionary. However, if either the monobehaviour or gameobject that possesses it no longer exist, naturally there is nothing that can be returned. Even if you make sure to remove the objects from the dictionary on their destruction and warn the user with a debug.log message, there is still the issue of having to return something. You can’t use anything like the Null Object pattern with monobehaviours because they have to be attached to a gameobject. What is the proper way to handle this kind of situation?
is it considered adequate to issue a debug.log warning and return null so that any subsequent nullrefexceptions show up in the user’s code rather than mine?
If you provide a piece of code that other users may use, it is the responsibility of the user to check for null pointers. But be sure to name your functions in a way that it reflect this behaviour. For example instead of using:
Something GetSomething(string name)
use:
bool TryGetSomething(string name, out Something result)
alright, sounds good. thank you.