• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jeepbumblejeep · Apr 06, 2019 at 11:13 PM · unityeditorcrashing

Unity crashes after saving code or after pressing play

Hello there, I was making a game that use speech recognition and I needed to use Task for concurrent execution. Well sometimes I have the problem that after saving modifications in the code or after playing the game, the unity editor sudenly closes. My code: using System; using System.Threading; using System.Threading.Tasks; using UnityEngine; using ReconocimientoDeVoz; using System.Collections.Generic;

 public class Movimiento : MonoBehaviour
 {
 
     public float velocidad = 0.1f;
     bool grabando = false;
     Task tareaAsincrona;
 
 
     // Start is called before the first frame update
     void Start()
     {
         reiniciarTarea();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         float direccionX = Input.GetAxisRaw("Horizontal");
         float direccionZ = Input.GetAxisRaw("Vertical");
 
         float posicionX = transform.position.x + (direccionX * velocidad * Time.deltaTime);
         float posicionZ = transform.position.z + (direccionZ * velocidad * Time.deltaTime);
         transform.position = new Vector3(posicionX, 0.5f, posicionZ);
 
 
         if (tareaAsincrona.IsCompleted)
         {
             reiniciarTarea();
         }
 
 
         if (Input.GetButtonDown("Fire1") && !grabando)
         {
             grabando = true;
             Debug.Log("Boton pulsado");
             tareaAsincrona.Start();
         }
 
     } 
     
     //Es para volver a asignar espacio a la tarea, puesto que una vez que termina debe eliminarse para no ocupar memoria
     void reiniciarTarea()
     {
         tareaAsincrona = new Task(() => {
             Debug.Log("Reconociendo");
             Task<List<string>> tareaReconocimiento = Reconocedor.reconocerVoz(5);
             List<string> listaReconocida = tareaReconocimiento.Result;
             tareaReconocimiento.Dispose();
 
             grabando = false;
             Debug.Log("No grabando");
             tareaAsincrona.Dispose();
         });
     }
 
 }

And the code from the speech recognizer (uses google speech to text): using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Google.Cloud.Speech.V1; using UnityEngine;

 namespace ReconocimientoDeVoz
 {
     public class Reconocedor
     {
 
         public static async Task<List<string>> reconocerVoz(int tiempo)
         {
             List<string> listaSoluciones = new List<string>();
             if (NAudio.Wave.WaveIn.DeviceCount < 1)
             {
                 Debug.Log("Sin microfono");
                 return listaSoluciones;
             }
             var speech = SpeechClient.Create();
             var streamingCall = speech.StreamingRecognize();
 
 
             //Configuración de petición inicial
             await streamingCall.WriteAsync(
                 new StreamingRecognizeRequest()
                 {
                     StreamingConfig = new StreamingRecognitionConfig()
                     {
                         Config = new RecognitionConfig()
                         {
                             Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                             SampleRateHertz = 16000,
                             LanguageCode = "es-ES",
                         },
                         InterimResults = true,
                         SingleUtterance = true //dejará de reconocer cuando se detecte que se ha dejado de hablar
                     }
                 }
                 );
 
             //Muestra las respuestas cuando llegan
             Task pintaRespuestas = Task.Run(async () =>
             {
                 while (await streamingCall.ResponseStream.MoveNext(default(CancellationToken)))
                 {
                     foreach (var result in streamingCall.ResponseStream.Current.Results)
                     {
                         foreach (var alternative in result.Alternatives)
                         {
                             Debug.Log(alternative.Transcript);
                             listaSoluciones.Add(alternative.Transcript);
                         }
                     }
                 }
             });
 
 
             //leer de microfono y enviar a la API
             object writeLock = new object();
             bool writeMore = true;
             var waveIn = new NAudio.Wave.WaveInEvent();
             waveIn.DeviceNumber = 0;
             waveIn.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
             waveIn.DataAvailable += (object sender, NAudio.Wave.WaveInEventArgs args) =>
             {
                 lock (writeLock)
                 {
                     if (!writeMore)
                     {
                         return;
                     }
                     streamingCall.WriteAsync(
                         new StreamingRecognizeRequest()
                         {
                             AudioContent = Google.Protobuf.ByteString.CopyFrom(args.Buffer, 0, args.BytesRecorded)
                         }
                         ).Wait();
                 }
             };
             waveIn.StartRecording();
             Debug.Log("Habla");
             await Task.Delay(TimeSpan.FromSeconds(tiempo));
             //deja de grabar y termina
             waveIn.StopRecording();
             lock (writeLock) writeMore = false;
             await streamingCall.WriteCompleteAsync();
             await pintaRespuestas;
 
             return listaSoluciones;
         }
 
 
 
     }
 }

As you can see, I dispose off the task, so the memory can be free, but I'm not sure if it's working well. I hope you can help me.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Searching for solution to AsyncOperation crashing Unity on stop playmode in Editor 0 Answers

Unity Crashing on opening gameplay scene 1 Answer

URGENT!!! - Unity keeps crashing 1 Answer

progressive gpu lightmapper keep crashing 1 Answer

Does increasing framerate to 5 has a negative impact on simulation? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges