SpeechRecognitionEngine.InstalledRecognizers() returns null within Unity

I am trying to use the speech recognition system from the .NET 3.0 framework within Unity. I therefore have imported the System.Speech.dll, from .NET 3.0 since higher version did not work, into the assets folder together with its dependencies. I know that what I am trying to do might not work because Unity uses .NET 2.0, but I still want to give it a try. It all seems to work and I can run the game fine, but when I run it I get the error:

 NullReferenceException: Object reference not set to an instance of an object
 System.Speech.Internal.ObjectTokens.RegistryDataKey.HKEYfromRegKey (Microsoft.Win32.RegistryKey regKey)
 System.Speech.Internal.ObjectTokens.RegistryDataKey.RootHKEYFromRegPath (System.String rootPath)
 System.Speech.Internal.ObjectTokens.RegistryDataKey.Open (System.String registryPath, Boolean fCreateIfNotExist)
 System.Speech.Internal.ObjectTokens.ObjectTokenCategory.Create (System.String sCategoryId)
 System.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers ()
 VoiceCommandListener.Start () (at Assets/Scripts/VoiceCommandListener.cs:14)

The line of code is:

 SpeechRecognitionEngine speechRecognitionEngine = new SpeechRecognitionEngine(SpeechRecognitionEngine.InstalledRecognizers()[0]);

When I run the same line of code outside of Unity it works fine and just grabs the default and first recognizer, but somehow it doesn't work within Unity.

I have tried switching between using .NET 2.0 instead of .NET 2.0 subset, switching to Microsoft.Speech.Recognition and importing the Microsoft.Speech.dll didn't work either and gives the same type of error. I also tried running the following code block instead:

 SpeechRecognitionEngine speechRecognitionEngine = null;
 foreach (RecognizerInfo info in SpeechRecognitionEngine.InstalledRecognizers()) {
     if (info.Culture.Equals(requiredCulture) && info.Id == requiredId ) {
         speechRecognitionEngine = new SpeechRecognitionEngine(info);
         break;
     }
 }

This gives the same result and this time again at the line of code which uses "SpeechRecognitionEngine.InstalledRecognizers()".

Does anybody know why it does not return anything?

Switching to Microsoft.Speech.Recognition and importing the Microsoft.Speech.dll didn't work either and gives the same type of error.

Thanks in advance.

Ron

Answers


As user3071284 pointed out, MS .NET and Mono are different in their requirements, and unity basically only allows .NET from a Microsoft .NET versions perspective. Even tho' it is running a higher .NET version from Mono's perspective.

This topic has been covered in details on the Unity Forums, I took a stab at it a few years ago, and came up with this. https://www.youtube.com/watch?v=CuLwwcekoi8 I no longer have the source code for that project, but it essentially used the UDP network protocol to communicate with a backend service running speech recognition on a higher .NET version, one thing you could do is look at UnityBridge Which essentially does the same thing, just makes it a bit more user-friendly. You can take a look at the manual.


I am running into the same issue, but what seems to be the case is that the Microsoft.Win32.RegistryKey class in the shipped Mono mscorlib is not fully compatible with the Microsoft version.

Having a look with ILSpy in the System.Speech dll it shows the method in which the null reference occurs tries to obtain the non-public 'hkey' field of the passed RegistryKey instance using reflection. This field is not available in the mscorlib shipped with Unity.


Need Your Help

CDI not working in ear deployed to glassfish v3.0.1

java-ee glassfish cdi ejb-3.1 java-ee-6

I have some problem with using weld logger injection.

How to add duplicate keys into the Dictionary

c# string dictionary key text-files

I have some lines from text files that i want to add into the Dictionary.I am using Dictionary for the first time.While adding up starting lines it was Ok but suddenly i got error: