- Home /
SpeechRecognitionEngine.InstalledRecognizers() returns null within Unity.
Hello,
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, or 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?
Edit: 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
Answer by Devacann · Dec 13, 2015 at 02:49 PM
You need to install Language Packs on windows.
the code
SpeechRecognitionEngine.InstalledRecognizers()
throw NullExceptions when you dont have any recognizer installed.
See there Add Language Packs to Windows
On Windows 10 it's simple:
Press Windows Key;
type "Settings" and press 'Enter';
select "Time & Language" then "Region & Language";
on "language" section choose "English" (Or Add);
click in "Options" button and download the "Speech" content.
Answer by RedKenrok · Dec 14, 2015 at 06:05 PM
I made sure it was installed and selected and still no difference. To note it works outside of Unity3D in a separate command line program just not within Unity3D itself.
Answer by jorrit5477 · Jun 18, 2016 at 05:47 AM
I am running into the same issue, but what could 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.
Since it runs within a regular C# console application, this has nothing to do with installed language packs.