• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jasper1 · Nov 30, 2015 at 02:42 PM · commandgenericsbehaviourcompiler-error

Command/ClientRpc with generic parameter

Hi, I have a problem using generics with behaviours in Unity (5.2.3f1). I implemented an abstract generic class which derives from NetworkBehaviour and has two members, a Command and a ClientRpc method. This is perfectly valid C# code, yet the Unity compiler fails with a NRE. Is this expected and if so, why?

My code:

     public abstract class MyGenericBehaviour<T> : NetworkBehaviour {
         [Command]
         protected virtual void CmdCommand(T param) {
             //...
         }
 
         [ClientRpc]
         protected virtual void RpcCall(T param) {
             //...
         }
     }

The Unity compiler gives the following error:

UNetWeaver error: Exception :System.NullReferenceException: Object reference not set to an instance of an object at Unity.UNetWeaver.NetworkBehaviourProcessor.ProcessMethods () [0x002c2] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetBehaviourProcessor.cs:1225 at Unity.UNetWeaver.NetworkBehaviourProcessor.Process () [0x00022] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetBehaviourProcessor.cs:48 at Unity.UNetWeaver.Weaver.ProcessNetworkBehaviourType (Mono.Cecil.TypeDefinition td) [0x0008b] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetWeaver.cs:1000 at Unity.UNetWeaver.Weaver.CheckNetworkBehaviour (Mono.Cecil.TypeDefinition td) [0x0008d] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetWeaver.cs:1477 at Unity.UNetWeaver.Weaver.Weave (System.String assName, IEnumerable`1 dependencies, IAssemblyResolver assemblyResolver, System.String unityEngineDLLPath, System.String unityUNetDLLPath, System.String outputDir) [0x00098] in C:\buildslave\unity\build\Extensions\Networking\Weaver\UNetWeaver.cs:1586 UnityEngine.Debug:LogError(Object) Unity.UNetWeaver.Log:Error(String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:20) Unity.UNetWeaver.Weaver:WeaveAssemblies(IEnumerable`1, IEnumerable`1, IAssemblyResolver, String, String, String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1683) Unity.UNetWeaver.Program:Process(String, String, String, String[], String[], IAssemblyResolver, Action`1, Action`1) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:34) UnityEditor.Scripting.Serialization.Weaver:WeaveUnetFromEditor(String, String, String, String, Boolean)

When I remove the Command/ClientRpc attribute or the generic method parameter, the compiler works as expected. I also noticed that using other attributes such as Server and ServerCallback work as expected.

Update

I tried to work around this issue by using a closed constructed type like this:

 public abstract class MyGenericBehaviour<T> : NetworkBehaviour {
     protected abstract void CmdCommand(T param);
     
     protected abstract void RpcCall(T param);
 }
 public abstract class MyGenericBehaviourOfFloat : MyGenericBehaviour<float> {
     [Command]
     protected override void CmdCommand(float param) {
         //...
     }
     [ClientRpc]
     protected override void RpcCall(float param) {
         //...
     }
 }
 public MyConcreteBehaviour : MyGenericBehaviourOfFloat {
     //...
 }

This compiles, but fails at runtime with the following Exception:

InvalidProgramException: Invalid IL code in Assets.Scripts.Utils.MyGenericBehaviourOfFloat :OnSerialize (UnityEngine.Networking.NetworkWriter,bool): IL_0003: call 0x060001b7

Assets.Scripts.MyConcreteBehaviour.OnSerialize (UnityEngine.Networking.NetworkWriter writer, Boolean forceAll) UnityEngine.Networking.NetworkIdentity.UNetSerializeAllVars (UnityEngine.Networking.NetworkWriter writer) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:421) UnityEngine.Networking.NetworkServer.SendSpawnMessage (UnityEngine.Networking.NetworkIdentity uv, UnityEngine.Networking.NetworkConnection conn) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1397) UnityEngine.Networking.NetworkServer.ShowForConnection (UnityEngine.Networking.NetworkIdentity uv, UnityEngine.Networking.NetworkConnection conn) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1226) UnityEngine.Networking.NetworkConnection.AddToVisList (UnityEngine.Networking.NetworkIdentity uv) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:502) UnityEngine.Networking.NetworkIdentity.AddObserver (UnityEngine.Networking.NetworkConnection conn) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:721) UnityEngine.Networking.NetworkIdentity.RebuildObservers (Boolean initialize) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:759) UnityEngine.Networking.NetworkServer.SpawnObject (UnityEngine.GameObject obj) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1354) UnityEngine.Networking.NetworkServer.Spawn (UnityEngine.GameObject obj) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1573) UnityEngine.Networking.NetworkServer.FinishPlayerForConnection (UnityEngine.Networking.NetworkConnection conn, UnityEngine.Networking.NetworkIdentity uv, UnityEngine.GameObject playerGameObject) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1080) UnityEngine.Networking.NetworkServer.InternalAddPlayerForConnection (UnityEngine.Networking.NetworkConnection conn, UnityEngine.GameObject playerGameObject, Int16 playerControllerId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1014) UnityEngine.Networking.NetworkServer.AddPlayerForConnection (UnityEngine.Networking.NetworkConnection conn, UnityEngine.GameObject player, Int16 playerControllerId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:971) UnityEngine.Networking.NetworkManager.OnServerAddPlayerInternal (UnityEngine.Networking.NetworkConnection conn, Int16 playerControllerId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManager.cs:816) UnityEngine.Networking.NetworkManager.OnServerAddPlayer (UnityEngine.Networking.NetworkConnection conn, Int16 playerControllerId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManager.cs:782) UnityEngine.Networking.NetworkManager.OnServerAddPlayerMessageInternal (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManager.cs:669) UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:421) UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:377) UnityEngine.Networking.NetworkConnection.TransportRecieve (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:527) UnityEngine.Networking.NetworkServer.InternalUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:723) UnityEngine.Networking.NetworkServer.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:585) UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:915)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

moving gameobject from state machine behaviour/different code result from inside OnStateExit() 1 Answer

static function generates error CS0119: 1 Answer

differences: generic GetComponent. 3 Answers

Count returning 0 when called outside object~ 1 Answer

Generic MonoBehaviour with nested class serialization issue 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges