NullReferenceException with SpeechSynthesizer in Unity

691 Views Asked by At

I'm using the C# SpeechSynthesizer in a Unity project, as in this example:

https://learn.microsoft.com/en-us/dotnet/api/system.speech.synthesis.speechsynthesizer?view=netframework-4.8

I am getting this error:

NullReferenceException: Object reference not set to an instance of an object
System.Speech.Internal.ObjectTokens.RegistryDataKey.HKEYfromRegKey (Microsoft.Win32.RegistryKey regKey) (at [...])
System.Speech.Internal.ObjectTokens.RegistryDataKey.RootHKEYFromRegPath (System.String rootPath) (at [...])
System.Speech.Internal.ObjectTokens.RegistryDataKey.Open (System.String registryPath, System.Boolean fCreateIfNotExist) (at [...])
System.Speech.Internal.ObjectTokens.ObjectTokenCategory.Create (System.String sCategoryId) (at [...])
System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut () (at [...])
System.Speech.Internal.Synthesis.VoiceSynthesis..ctor (System.WeakReference speechSynthesizer) (at [...])
System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer () (at [...])
System.Speech.Synthesis.SpeechSynthesizer.SelectVoiceByHints (System.Speech.Synthesis.VoiceGender gender, System.Speech.Synthesis.VoiceAge age, System.Int32 voiceAlternate, System.Globalization.CultureInfo culture) (at [...])
System.Speech.Synthesis.SpeechSynthesizer.SelectVoiceByHints (System.Speech.Synthesis.VoiceGender gender) (at [...])

This is my class:

using System;
using System.Speech.Synthesis;
using UnityEngine;

    public class Speaker : MonoBehaviour
    {
        private SpeechSynthesizer synth = new SpeechSynthesizer();

        void Start() {        
            synth.SetOutputToDefaultAudioDevice();  
            synth.SelectVoiceByHints(VoiceGender.Female);
        }

      }

Any ideas are welcome.

0

There are 0 best solutions below