Problem creating templated instances derived from NetworkBehaviour
Description
Creating a templated instance derived from NetworkBehaviour doesn't appear to compile. I'm following along with this youtube, which links to this git repo
Reproduce Steps
- Create a templated class derived from NetworkBehaviour like
using UnityEngine;
using Unity.Netcode;
public class Singleton<T> : NetworkBehaviour
where T : Component
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
var objs = FindObjectsOfType(typeof(T)) as T[];
if (objs.Length > 0)
_instance = objs[0];
if (objs.Length > 1)
{
Debug.LogError("There is more than one " + typeof(T).Name + " in the scene.");
}
if (_instance == null)
{
GameObject obj = new GameObject();
obj.name = string.Format("_{0}", typeof(T).Name);
_instance = obj.AddComponent<T>();
}
}
return _instance;
}
}
}
- Create a class from Singleton like so
using Unity.Netcode;
public class PlayerManager : Singleton<PlayerManager>
{
NetworkVariable<int> playersInGame = new NetworkVariable<int>();
public int PlayersInGame
{
get
{
return playersInGame.Value;
}
}
}
Actual Outcome
Sadness
(0,0): error - System.NullReferenceException: Object reference not set to an instance of an object.|| at Unity.Netcode.Editor.CodeGen.INetworkSerializableILPP.Process(ICompiledAssembly compiledAssembly) in blah\Library\PackageCache\[email protected]\Editor\CodeGen\INetworkSerializableILPP.cs:line 116 at Unity.Netcode.Editor.CodeGen.INetworkSerializableILPP.Process(ICompiledAssembly compiledAssembly) in blah\Library\PackageCache\[email protected]\Editor\CodeGen\INetworkSerializableILPP.cs:line 116
Expected Outcome
Joy Being able to create a class, deriving from a templated networkbehaviour. Presumably this worked in previous iterations of Netcode as it's working in the tutorial video (created circa 5months ago)
Screenshots
If applicable, add screenshots to help explain your problem.
Environment
- OS: Windows 11
- Unity Version: 2021.3.2f1
- Netcode Version: 1.0.0-pre.8
- Netcode Commit: [e.g. https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/commit/38a1510e155e32f9f4f31d218c704e0129c221ca]
Additional Context
The above linked (presumably previously working) repo looks to use "com.unity.netcode.gameobjects": "1.0.0-pre.3",
This issue doesn't appear using "com.unity.netcode.gameobjects": "1.0.0-pre.7"
but is using "com.unity.netcode.gameobjects": "1.0.0-pre.8" and "com.unity.netcode.gameobjects": "1.0.0-pre.9"