Search code examples
unity-game-enginespeech-to-textwatson

How do I fix this error when trying to use the Watson SDK for Unity?


thanks for taking a look at this. I'm new to Unity and struggling to understand how to fix this error.

Assets/Watson/Scripts/Logging/Logger.cs(115,53): error CS0311: The type IBM.Watson.DeveloperCloud.Logging.LogSystem' cannot be used as type parameter T in the generic type or method Singleton. There is no implicit reference conversion from IBM.Watson.DeveloperCloud.Logging.LogSystem' to `UnityEngine.MonoBehaviour

This is the line causing the error:

public static LogSystem Instance { get { return Singleton<LogSystem>.Instance; } }

And this is it in context:

 /// <summary>
  /// This singleton class maintains the of list of installed reactors and handles all LogRecord 
  /// objects. See the static class Log for functions the end user of this system should actually
  /// be calling. This class is thread safe.
  /// </summary>
  public class LogSystem
  {
    #region Public Properties
    /// <summary>
    /// Returns the singleton instance of the Logger object.
    /// </summary>
    **public static LogSystem Instance { get { return Singleton<LogSystem>.Instance; } }**
    #endregion

    #region Private Data
    private static bool sm_bInstalledDefaultReactors = false;
    List<ILogReactor> m_Reactors = new List<ILogReactor>();
    #endregion

    #region Public Functions
    public static List<ILogReactor> ReactorsInstalled
    {
      get
      {
        return LogSystem.Instance.m_Reactors;
      }
    }

    /// <summary>
    /// Install a default debug and file reactor.
    /// </summary>
    public static void InstallDefaultReactors(int logHistory = 2, LogLevel logLevelFileReactor = LogLevel.STATUS)
    {
      if (!sm_bInstalledDefaultReactors)
      {
        // install the default reactors...
        sm_bInstalledDefaultReactors = true;
#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID
        LogSystem.Instance.InstallReactor(new DebugReactor());
#endif

        if (!string.IsNullOrEmpty(Constants.Path.LOG_FOLDER) && !System.IO.Directory.Exists(Application.persistentDataPath + Constants.Path.LOG_FOLDER))
          System.IO.Directory.CreateDirectory(Application.persistentDataPath + Constants.Path.LOG_FOLDER);

        LogSystem.Instance.InstallReactor(new FileReactor(Application.persistentDataPath + Constants.Path.LOG_FOLDER + "/" + Application.productName + ".log", logLevelFileReactor, logHistory));

        Application.logMessageReceived += UnityLogCallback;
      }
    }

    static void UnityLogCallback(string condition, string stacktrace, LogType type)
    {
      if (type == LogType.Exception)
        Log.Critical("Unity", "Unity Exception {0} : {1}", condition, stacktrace);
    }

    /// <summary>
    /// Installs a reactor into this Logger.
    /// </summary>
    /// <param name="reactor">The reactor object.</param>
    public void InstallReactor(ILogReactor reactor)
    {
      lock (m_Reactors)
      {
        m_Reactors.Add(reactor);
      }
      // set our default reactor flag to true if the user installs their own reactors.
      sm_bInstalledDefaultReactors = true;
    }

    /// <summary>
    /// Removes a reactor from this Logger.
    /// </summary>
    /// <param name="reactor">The reactor to remove.</param>
    /// <returns>Returns true on success.</returns>
    public bool RemoveReactor(ILogReactor reactor)
    {
      lock (m_Reactors)
      {
        return m_Reactors.Remove(reactor);
      }
    }

    /// <summary>
    /// Send the given LogRecord to all installed reactors.
    /// </summary>
    /// <param name="log">The LogRecord to pass to all reactors.</param>
    public void ProcessLog(LogRecord log)
    {
      lock (m_Reactors)
      {
        foreach (var reactor in m_Reactors)
          reactor.ProcessLog(log);
      }
    }
    #endregion
  }

Solution

  • public static LogSystem Instance 
    { 
         get 
         { 
              return instance; 
         } 
    }
    private static LogSystem instance;
    // Ctor of the class
    public LogSystem()
    { 
        if(instance == null) { instance = this;}
    }
    

    This is a very basic singleton pattern implementation. You can find more advanced ones online.