"NetworkBehaviour cannot have generic parameters" - Why?

For some reason, classes that derive from NetworkBehaviour cannot have generic parameters. Why is that?
This is a showstopper for us at the moment as our MVC implementation makes use of generic parameters and we therefor cannot implement a View that is capable of using unet.

Create an empty project, create a new C# script, then paste this to get the error:

using UnityEngine.Networking;

public class NetworkTest<T> : NetworkBehaviour {
}

[edit]
This is happening on 5.3.4f1 but is marked as fixed in the issue tracker:

Ok, we created a new class, NetworkBehaviour2 and copy/pasted the code from here:
https://bitbucket.org/Unity-Technologies/networking/src/3610ab4e4c0f2a3d74bb3d005641e11201c7e6f3/Runtime/NetworkBehaviour.cs?at=5.3&fileviewer=file-view-default

We thought we might get a bit more info on the error then.
Turned out, using the code from the link, we can derive from NetworkBehaviour2 using generic parameters just fine. But then again, that version of NetworkBehaviour is from 2015-12-11 (5.3.0f4) so it might be outdated (?).

Any hints? We are not very keen on using a custom NetworkBehaviour, but can live with it for the moment.

Nevermind, the above “solution” will not work as the UNet Weaver does not acknowledge a custom NetworkBehaviour as NetworkBehaviour and therefor [Command] [SyncVar] [ClientRcp] etc. will not work.

Still looking for a solution. Any help appreciated :slight_smile:

Im confused as to what you are trying to do. Are you trying to have a generic monobehaviour?
I dont think unity can handle generic components, but a possible workaround might be to inherit from your generic component and make it non generic.
For example

Public class NetworkTestInt : NetworkTest<int>
{

}

Now it isnt generic and should work fine (not tested), although its not as nice to use.
I think the reason for this is that Unity has trouble serializing generics.

Also here is a post of someone asking about generic networkbehaviour, and unity replied with there being no support for this.

In fact, he was the one that issued that bug report you posted above. It seems he was only fixing an error related to generics, but not making generics work or anything.

Thanks for your answer HiddenMonk.

We are trying to derive from NetworkBehaviour, but the derived class should have a generic parameter, which is not possible for UNet NetworkBehaviours. So your example also won’t work, as NetworkTest needs to derive from NetworkBehaviour (which won’t work) :slight_smile:

Thanks for pointing out that post, at least now we know that this is not a bug but a “feature” of some sort :wink:
We’ll create a permanent workaround then for our MVC implementation when it comes to networked Views.

2 Likes

Hey Gentlymad, having the same problem here, can’t even implement a basic singleton version of the NetworkBehaviour class. Bump for Unity attention! :slight_smile:

EDIT: Shame on me for not properly reading all the replies, I see now that this is not something to be fixed, thank you HiddenMonk!

2 Likes