Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Error : Can not play a disabled audio source

Discussion in 'Getting Started' started by Yair123456789, Jan 16, 2024.

  1. Yair123456789

    Yair123456789

    Joined:
    Dec 21, 2021
    Posts:
    12
    Hi Im currently working on a quiz game. I have a music category where a song plays and you need to guess who sang it. But the first audio gives me the following error:

    Can not play a disabled audio source
    UnityEngine.AudioSource:playOneShot (UnityEngine.AudioClip)
    QuizUI/<PlayAudio>d__30:MoveNext () (at Assets/Quiz/Scripts/QuizUI.cs:135)
    UnityEngine.MonoBehaviour:StartCoroutine (System.Collections.IEnumerator)
    QuizUI:SetQuestion (Question) (at Assets/Quiz/Scripts/QuizUI.cs:112)
    QuizManager:SelectQuestion () (at Assets/Quiz/Scripts/QuizManager.cs:42)
    QuizManager:StartGame (int) (at Assets/Quiz/Scripts/QuizManager.cs:33)
    QuizUI:OnClick (UnityEngine.UI.Button) (at Assets/Quiz/Scripts/QuizUI.cs:188)
    QuizUI/<>c__DisplayClass26_1:<Awake>b__1 () (at Assets/Quiz/Scripts/QuizUI.cs:39)
    UnityEngine.EventSystems.EventSystem:Update ()

    The other audios play fine. Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. public class QuizUI : MonoBehaviour
    7. {
    8.     [SerializeField] private QuizManager quizManager;
    9.     [SerializeField] private Text questionText, scoreText, timerText;
    10.     [SerializeField] private List<Image> lifeImageList;
    11.     [SerializeField] private GameObject gameOverPanel, mainMenuPanel, gameMenuPanel;
    12.     [SerializeField] private Image questionImage;
    13.     [SerializeField] private UnityEngine.Video.VideoPlayer questionVideo;
    14.     [SerializeField] private AudioSource questionAudio;
    15.     [SerializeField] private List<Button> options, uiButtons;
    16.     [SerializeField] private Color correctCol, wrongCol, normalCol;
    17.  
    18.     private Question question;
    19.     private bool answered;
    20.     private float audioLenght;
    21.     private bool showCorrectAnswer = false;
    22.  
    23.     public Text ScoreText { get { return scoreText; } }
    24.     public Text TimerText { get { return timerText; } }
    25.  
    26.     public GameObject GameOverPanel { get { return gameOverPanel; } }
    27.  
    28.     // Start is called before the first frame update
    29.     void Awake()
    30.     {
    31.         for (int i = 0; i < options.Count; i++)
    32.         {
    33.             Button localBtn = options[i];
    34.             localBtn.onClick.AddListener(() => OnClick(localBtn));
    35.         }
    36.         for (int i = 0; i < uiButtons.Count; i++)
    37.         {
    38.             Button localBtn = uiButtons[i];
    39.             localBtn.onClick.AddListener(() => OnClick(localBtn));
    40.         }
    41.     }
    42.  
    43.     public void ShowCorrectAnswer(string correctAnswer)
    44.     {
    45.         StartCoroutine(ShowCorrectAnswerCoroutine(correctAnswer));
    46.     }
    47.  
    48.     // Coroutine to handle the color change
    49.     private IEnumerator ShowCorrectAnswerCoroutine(string correctAnswer)
    50.     {
    51.         Button correctButton = null;
    52.  
    53.         // Find the correct answer button
    54.         foreach (Button optionButton in options)
    55.         {
    56.             if (optionButton.name == correctAnswer)
    57.             {
    58.                 correctButton = optionButton;
    59.                 break;
    60.             }
    61.         }
    62.  
    63.         if (correctButton != null)
    64.         {
    65.             correctButton.image.color = Color.green;
    66.         }
    67.  
    68.         // Wait for a short duration (e.g., 1 second)
    69.         yield return new WaitForSeconds(1f);
    70.  
    71.         // Reset the color to normal
    72.         if (correctButton != null)
    73.         {
    74.             correctButton.image.color = normalCol;
    75.         }
    76.  
    77.         showCorrectAnswer = false;
    78.     }
    79.  
    80.  
    81.  
    82.     public void SetQuestion(Question question)
    83.     {
    84.         this.question = question;
    85.  
    86.         switch (question.questionType)
    87.         {
    88.             case QuestionType.TEXT:
    89.  
    90.                 questionImage.transform.parent.gameObject.SetActive(false);
    91.  
    92.                 break;
    93.             case QuestionType.IMAGE:
    94.                 ImageHolder();
    95.                 questionImage.transform.gameObject.SetActive(true);
    96.  
    97.                 questionImage.sprite = question.qustionImg;
    98.                 break;
    99.             case QuestionType.VIDEO:
    100.                 ImageHolder();
    101.                 questionVideo.transform.gameObject.SetActive(true);
    102.  
    103.                 questionVideo.clip = question.qustionVideo;
    104.                 questionVideo.Play();
    105.                 break;
    106.             case QuestionType.AUDIO:
    107.                 ImageHolder();
    108.                 questionAudio.transform.gameObject.SetActive(true);
    109.  
    110.                 audioLenght = question.qustionClip.length;
    111.  
    112.                 StartCoroutine(PlayAudio());
    113.  
    114.                 break;
    115.         }
    116.  
    117.         questionText.text = question.questionInfo;
    118.  
    119.         List<string> answerList = ShuffleList.ShuffleListItems<string>(question.options);
    120.  
    121.         for (int i = 0; i < options.Count; i++)
    122.         {
    123.             options[i].GetComponentInChildren<Text>().text = answerList[i];
    124.             options[i].name = answerList[i];
    125.             options[i].image.color = normalCol;
    126.         }
    127.  
    128.         answered = false;
    129.     }
    130.  
    131.     IEnumerator PlayAudio()
    132.     {
    133.         if (question.questionType == QuestionType.AUDIO)
    134.         {
    135.             questionAudio.PlayOneShot(question.qustionClip);
    136.  
    137.             yield return new WaitForSeconds(audioLenght + 0.5f);
    138.  
    139.             StartCoroutine(PlayAudio());
    140.         }
    141.         else
    142.         {
    143.             StopCoroutine(PlayAudio());
    144.             yield return null;
    145.         }
    146.     }
    147.  
    148.     void ImageHolder()
    149.     {
    150.         questionImage.transform.parent.gameObject.SetActive(true);
    151.         questionImage.transform.gameObject.SetActive(false);
    152.         questionAudio.transform.gameObject.SetActive(false);
    153.         questionVideo.transform.gameObject.SetActive(false);
    154.     }
    155.  
    156.     private void OnClick(Button btn)
    157.     {
    158.  
    159.         if (quizManager.GameStatus == GameStatus.Playing)
    160.         {
    161.  
    162.             if (!answered)
    163.             {
    164.                 answered = true;
    165.                 bool val = quizManager.Answer(btn.name);
    166.  
    167.                 if (val)
    168.                 {
    169.                     btn.image.color = correctCol;
    170.                 }
    171.                 else
    172.                 {
    173.                     btn.image.color = wrongCol;
    174.                 }
    175.             }
    176.         }
    177.    
    178.  
    179.  
    180.         switch (btn.name)
    181.         {    
    182.             case "היפרגואג":
    183.                 quizManager.StartGame(0);
    184.                 mainMenuPanel.SetActive(false);
    185.                 gameMenuPanel.SetActive(true);
    186.                 break;
    187.             case "הקיזומ":
    188.                 quizManager.StartGame(1);
    189.                 mainMenuPanel.SetActive(false);
    190.                 gameMenuPanel.SetActive(true);
    191.                 break;
    192.             case "יללכ עדי":
    193.                 quizManager.StartGame(2);
    194.                 mainMenuPanel.SetActive(false);
    195.                 gameMenuPanel.SetActive(true);
    196.                 break;
    197.         }
    198.     }
    199.  
    200.     public void RetryButton()
    201.     {
    202.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    203.     }
    204.  
    205.     public void ReduceLife(int index)
    206.     {
    207.         lifeImageList[index].color = wrongCol;
    208.     }
    209.  
    210. }
    211.  
    image_2024-01-16_114958769.png