I have been using the following base class for singleton components:
using UnityEngine;
using System.Collections;
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
protected static T instance;
/**
Returns the instance of this singleton.
*/
public static T Instance
{
get
{
if(instance == null)
{
instance = (T) FindObjectOfType(typeof(T));
if (instance == null)
{
Debug.Log(typeof(T) +
" instance is missing.");
}
}
return instance;
}
}
}
Today I tried making a virtually identical class that inherited from NetworkBehavior because I had some networked game manager scripts that I wanted easy access to. So I just switched out MonoBehavior for NetworkBehavior. Things built fine in mlnodevelop, but the editor console immediately gave me. “UNetWeaver error: NetworkBehaviour NetworkSingleton`1 cannot have generic parameters,” and reported failure generating network code.
I’m curious what is causing this, and would like to know if there is an easy way implement singleton a singleton base class for network behaviors.