NetworkObjectReference can't be NULL though that seems like a perfectly valid use case

Hi!

I was scratching my head a bit working on some pickup/throwing mechanics for my game, and I realized that NetworkObjectReference throws an exception (which my ability system was kindly eating and not rethrowing, whoops ^_^) if the NetworkObject passed to the constructor is NULL.

My exact use-case is this:
When a character picks up an object, I’m tracking the carried object via a NetworkVariable and using that as a quick “validation” mechanism for networked gameplay. I also have some other code (namely the “drop object” ability) that checks for a valid carried object and sets it to NULL. This is the broken bit because NetworkObjectReference CAN’T be NULL.

I’ve done this a lot with other abilities I’ve built as a really quick form of Server RPC receipt validation, but this is the first time I’ve used NetworkObjectReference like this.

This seems like a totally valid use case to me that would be sweet if supported! Right now I’m using this as a NetworkVariable, so as a workaround I added a NullableNetworkObjectReference type that’s copy+pasta of NetworkObjectReference, just with NULL support :wink:

Thoughts?

Hi @UnkelRambo , this seems a valid use case, thanks for sharing!

Would you mind opening a feature request on the repository of Netcode For Gameobjects (you can do it from this link) and paste its link here?

In this way, the NGO team will be able to prioritize this feature for future versions of the SDK.

1 Like

Definitely, thanks!

Submitted here:

I also just hit a similar use-case trying to parent a NetworkObject to NULL, which is stated as a valid case in the documentation but the source explicitly disallows NULL parents. Should I open a separate request for that or add it to the request for NetworkObjectReference?

Thanks again :slight_smile:

Edit: In fact, funny enough, the “parenting to NULL” case ISN’T handled in the BossRoom demo drop action, as described here:

Just got the same problem during a UNET migration. At some point a RPC send a GameObject that could be null
(in our case something like ‘Destroyed( GameObject src)’ and having no src is perfectly fine)
Currently need to split into 2 rpcs or using a special ‘empty’ gameobject created for this : no very handy

Thank you so much, @UnkelRambo !

Yes, please! It’s the easiest way to make the team aware of the issue, especially if you can back the issue with a code sample

Oooh, nice catch! I’m reporting this to Boss Room’s team

Fully agree, already raised this all the way back in February :wink:

https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/1693