Search Unity

Can not play a disabled audio source

Discussion in 'Scripting' started by Abid123, Mar 17, 2019.

  1. Abid123

    Abid123

    Joined:
    Mar 17, 2019
    Posts:
    5
    I am trying to get this voice recording to play however the following message is appearing. I am a newbie so any help would be appreciated. it says the error is on line 36

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Playsound : MonoBehaviour
    6. {
    7.  
    8.     GameObject BPlay, BStop;
    9.  
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         BPlay = GameObject.Find("Play");
    16.         BStop = GameObject.Find("Stop");
    17.      
    18.         BPlay.SetActive(true);
    19.         BStop.SetActive(false);
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if (Input.GetMouseButtonDown(0))
    27.         {
    28.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    29.             RaycastHit hit;
    30.             if (Physics.Raycast(ray, out hit))
    31.             {
    32.                 if (hit.collider.tag == "Play")
    33.                 {
    34.                     BPlay.SetActive(false);
    35.                     BStop.SetActive(true);
    36.                     BPlay.GetComponent<AudioSource>().Play();
    37.                 }
    38.                 if (hit.collider.tag == "Stop")
    39.                 {
    40.                     BPlay.SetActive(true);
    41.                     BStop.SetActive(false);
    42.                     BPlay.GetComponent<AudioSource>().Stop();
    43.  
    44.  
    45.                 }
    46.             }
    47.         }
     
  2. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    3,023
    First thing to do is restructure your code. Don't use GetComponent in Update, because that will waste performance. Cache a reference to the audio component in a class level variable, and then manually set that variable at design time or use GetComponent in the Start or Awake function.

    Beyond that, does the BPlay game object definitely have an audio component, and is that audio component enabled?

    Next, since you are disabling the BPlay game object sometimes, it is possible that your BPlay game object is currently disabled when you are trying to access it's audio component.
     
    Abid123 likes this.
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Wrong forum (use scripting) but you are deactivating the GameObject right before you try to play its AudioSource. Put the audio on a different game object, or disable the button/sprite instead of deactivating the GO.
     
    Abid123 and angrypenguin like this.
  4. Abid123

    Abid123

    Joined:
    Mar 17, 2019
    Posts:
    5
    thank you both for the help The error has been however it does not play the sound
    thank you i am trying to fix it no luck at the moment very new at coding. how do I set disable the sprite what code do need to added
     
  5. Abid123

    Abid123

    Joined:
    Mar 17, 2019
    Posts:
    5
    Thank you mate managed to get it working thank you guys
     
  6. TimothyGates

    TimothyGates

    Joined:
    Jan 31, 2019
    Posts:
    19
    How did you get it working?, been at it a few days moving things around