Skip to content

Using Generics with ServerRpc will generate error at line (0,0) #1860

@ddonk

Description

@ddonk

Descripition

I was trying to make a handler that can sync variables over network, my only problem is that when I use a method with the ServerRpc attribute it gives my a compile error.

Reproduce Steps

  1. Create a script with this as the code:
public class NetworkHandler<T> : NetworkBehaviour where T : unmanaged
{
    private NetworkVariable<T> _networkVariable = new NetworkVariable<T>();
    public void InitGeneric(T value)
    {
        Debug.Log("Testing Generic Method");
    }

    public void SetValue(T value)
    {
        if(!IsOwner) return;

        if (NetworkManager.Singleton.IsServer)
        {
            _networkVariable.Value = value;
        }
        else
        {
            SubmitRequestServerRpc(value);
        }
    }
    
    [ServerRpc]
    void SubmitRequestServerRpc(T value)
    {
        //TODO implement Rpc logic
    }
}
  1. Save Script so Unity will recompile
  2. See Error

Actual Outcome

This is the error i get:
(0,0): error - System.NullReferenceException: Object reference not set to an instance of an object.|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.GetWriteMethodForParameter(TypeReference paramType, MethodReference& methodRef) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 621|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomAttribute rpcAttribute, UInt32 rpcMethodId) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 1014|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.ProcessNetworkBehaviour(TypeDefinition typeDefinition, String[] assemblyDefines) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 409|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.<>c__DisplayClass5_0.<Process>b__1(TypeDefinition b) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 62|| at System.Collections.Generic.List1.ForEach(Action1 action)|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.Process(ICompiledAssembly compiledAssembly) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 59 at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.GetWriteMethodForParameter(TypeReference paramType, MethodReference& methodRef) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 621|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomAttribute rpcAttribute, UInt32 rpcMethodId) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 1014|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.ProcessNetworkBehaviour(TypeDefinition typeDefinition, String[] assemblyDefines) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 409|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.<>c__DisplayClass5_0.<Process>b__1(TypeDefinition b) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 62|| at System.Collections.Generic.List1.ForEach(Action1 action)|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.Process(ICompiledAssembly compiledAssembly) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 59

Expected Outcome

I expected unity wouldnt have given my any compile errors because i see no issues with what could go wrong at compile level. I was going to use this code to deploy on other scripts to be able to sync variables over network.

Environment

OS: Windows 10 Education
Unity Version: 2021.2.13f1
Netcode Version: 1.0.0-pre.6 - March 03, 2022

Additional Context

I suspect that a Rpc with generics is giving the error but my implentation of the code above would be like this , which would give it a hard type at compile time because of inheritance so I see no issue of my implemetation:

public class NetworkTest : NetworkHandler<int>
{
    private Random _random;
    private void Start()
    {
        _random = new Random();
        Debug.Log("Testing Less Generic Method");
        InitGeneric(10);
    }

    private void Update()
    {
        //Post a random value and sync over network
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SetValue(_random.Next(1, 20));
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:supportQuestions or other support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions