- Home /
Passing 'null' as parameter to SendMessage/BroadcastMessage
Is it not possible to use null
as the parameter for SendMessage/BroadcastMessage
?
When I try to pass null
I get the error 'Failed to call function SetSelectedNode of class PLC_Base. Calling function SetSelectedNode with no parameters but the function requires 1'
The use case is the situation where I am selecting a node from a set of nodes and the parameter is the GameObject
of the node to be selected. Passing null
should result in no node being selected.
I know there are a number of workarounds: - passing a dummy value that is no node's gameObject
instead of null
- overloading SetSelectedNode such that SetSelectedNode()
calls SetSelectedNode(null)
- passing an object with one field, that being the node to be selected
But these are all subpar compared to being able to use SendMessage/BroadcastMessage
properly.
Answer by Fattie · Jul 05, 2015 at 08:49 AM
It looks like you simply cannot send "null" as a parameter.
Example,
http://forum.unity3d.com/threads/sendmessage-will-null-as-value-method-not.66756/
It's really silly - but there it is.
Answer by Justin Warner · Mar 28, 2011 at 02:12 AM
If sending a string, send "". It's = to null... If you're passing a number, pass a negative number, and check it in the place where the number is accepted and handle it accordingly... Same with other datatypes... Good luck!
Unfortunately it is an object reference, so the only proper value in this situation really is null. Sending through the empty string or a negative number just results in errors...
Can you send over an empty gameobject? Then the user wont see it, and it'll just choose that one, you know?
That does seem better than the other workarounds I have, thanks.