Search Unity

"NetworkBehaviour cannot have generic parameters" - Why?

Discussion in 'Multiplayer' started by Gentlymad, Apr 27, 2016.

  1. Gentlymad

    Gentlymad

    Joined:
    Nov 11, 2014
    Posts:
    16
    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:

    Code (CSharp):
    1. using UnityEngine.Networking;
    2.  
    3. public class NetworkTest<T> : NetworkBehaviour {
    4. }

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

    https://issuetracker.unity3d.com/is...with-generic-parameter-cause-unetweaver-error
     
    Last edited: Apr 27, 2016
  2. Gentlymad

    Gentlymad

    Joined:
    Nov 11, 2014
    Posts:
    16
    Ok, we created a new class, NetworkBehaviour2 and copy/pasted the code from here:
    https://bitbucket.org/Unity-Technol...aviour.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.
     
  3. Gentlymad

    Gentlymad

    Joined:
    Nov 11, 2014
    Posts:
    16
    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 :)
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    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
    Code (CSharp):
    1. Public class NetworkTestInt : NetworkTest<int>
    2. {
    3.  
    4. }
    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.
    http://forum.unity3d.com/threads/generic-network-behaviours.354754/
    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.
     
  5. Gentlymad

    Gentlymad

    Joined:
    Nov 11, 2014
    Posts:
    16
    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<int> needs to derive from NetworkBehaviour (which won't work) :)

    Thanks for pointing out that post, at least now we know that this is not a bug but a "feature" of some sort ;)
    We'll create a permanent workaround then for our MVC implementation when it comes to networked Views.
     
    Deleted User and HiddenMonk like this.
  6. achynes

    achynes

    Joined:
    May 6, 2015
    Posts:
    7
    Hey Gentlymad, having the same problem here, can't even implement a basic singleton version of the NetworkBehaviour class. Bump for Unity attention! :)

    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!
     
    Last edited: Aug 15, 2016
    Sungold and HiddenMonk like this.