Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Not catching an error with a try/catch block...

Discussion in 'Scripting' started by Teedo145, Oct 17, 2017.

  1. Teedo145

    Teedo145

    Joined:
    Apr 14, 2014
    Posts:
    23
    Hi guys,
    I am trying to catch any errors while recording/accessing the mic but it doesn't seem to catch this error and then the game breaks and I can't click anymore buttons because of it.
    Here is the code and error...does anyone know why? :-(

    Code (CSharp):
    1. try {
    2.                 OnlineGameManager.Instance.currentGame.player1.attempt = Microphone.Start(null, false, (int)OnlineGameManager.Instance.currentGame.gameSound.audioClip.length + 1, 24000);
    3.                 _secsToWait = OnlineGameManager.Instance.currentGame.player1.attempt.length;
    4.             } catch {
    5.                 _success = false;
    6.             }
    Code (CSharp):
    1. Failed to get record driver caps. result=60 (Error initializing output device. )
    2. UnityEngine.Microphone:Start(String, Boolean, Int32, Int32)
    3. <OnButtonRecordAttemptCoroutine>c__Iterator0:MoveNext() (at Assets/Scripts/GUI/Pages/OnlineHaveTurnPage.cs:68)
    4. UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
    5.  
    Thanks :)
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Tonymotion likes this.
  3. Teedo145

    Teedo145

    Joined:
    Apr 14, 2014
    Posts:
    23
    The error is a 'error initializing output device' and occurs before i could do a null check on it. I just checked and it actually IS catching the error and _success is set to false, but the error still occurs and breaks the game :(
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    In what way?
     
  5. Teedo145

    Teedo145

    Joined:
    Apr 14, 2014
    Posts:
    23
    Ah I think I found the problem :)
    I had a gui message pop up with an "OK" button, but the OK button wasn't clickable and I thought that was because of the error...but it's because of my own code and i wasn't setting a bool back to false.
    I haven't been able to double check if that WAS it because the error is intermittent on my PC but I am pretty sure that was it :)
    Thanks
     
  6. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    Yea never assume things, I can speak of experience :) Always make sure if things really happen or not by adding a log in catch for instance, at least during development.