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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can not play a disabled audio source problem

Discussion in 'Scripting' started by Harardin, Aug 27, 2015.

  1. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Hello to everyone.

    I have a small problem. I created a few scripts to play sounds when OnTriggerEnter and Interract with an object.

    When on TriggerEnter everything works good. But when Ray casting it give me this error:

    Can not play a disabled audio source

    UnityEngine.AudioSource:playOneShot(AudioClip)

    DialogSoundPlay:BlackHandSound() (at Assets/Player/PlayerScripts/DialogSoundPlay.cs:22)

    BlackHandScript:GetAudioInDialog() (at Assets/chapter4/ActiveHandsScripts/BlackHandScript.cs:9)

    DoorRayCast:Update() (at Assets/doors_active/DoorRayCast.cs:119)

    Can Someone please help me to find where I made a mistake?

    Here is the first script it RayCasts it’s a little messy.

    Code (CSharp):
    1. public class DoorRayCast : MonoBehaviour
    2. {
    3.     public GameObject ePressText;
    4.     public float RayDistanse = 3f;
    5.     private float PhoneRayDistance = 3f;
    6.     private float CeilingDistance = 5f;
    7.     public phoneAnimationScript myphoneAnimationScript;
    8.     public phoneAnimationScript myCharacterAnimationScript;
    9.     public RelocateScript Relocate;
    10.     Camera CameraMain;
    11.  
    12.     public void Start()
    13.     {
    14.         CameraMain = GetComponent<Camera>();
    15.         ePressText.SetActive(false);
    16.     }
    17.     public void Update()
    18.     {
    19.         RaycastHit hit;
    20.         Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    21.         if (Physics.Raycast(ray, out hit, RayDistanse))
    22.         {
    23.             if (hit.transform.tag == "DoorTag")
    24.             {
    25.                 ePressText.SetActive(true);
    26.                 if (Input.GetButtonDown("actButton"))
    27.                 {
    28.                     hit.collider.GetComponent<DoorsActAnim>().DoorTrigger();
    29.                 }
    30.             }
    31.         }
    32.         else
    33.         {
    34.             ePressText.SetActive(false);
    35.         }
    36.         RaycastHit closedHit;
    37.         Ray closedRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    38.         if (Physics.Raycast(closedRay, out closedHit, RayDistanse))
    39.         {
    40.             if (hit.transform.tag == "ClosedDoor")
    41.             {
    42.                 ePressText.SetActive(true);
    43.                 if (Input.GetButtonDown("actButton"))
    44.                 {
    45.                     hit.collider.GetComponent<ClosedDoor>().DoorTriggerClosed();
    46.                 }
    47.             }
    48.         }
    49.         else
    50.         {
    51.             ePressText.SetActive(false);
    52.         }
    53.         RaycastHit cellPhone;
    54.         Ray phoneRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    55.         if (Physics.Raycast(phoneRay, out cellPhone, PhoneRayDistance))
    56.         {
    57.             if (hit.transform.gameObject.tag == "Phone")
    58.             {
    59.                 ePressText.SetActive(true);
    60.                 if (Input.GetButtonDown("actButton"))
    61.                 {
    62.                     phoneAnimationScript pas = GetComponent<phoneAnimationScript>();
    63.                     StartCoroutine(myphoneAnimationScript.CutSceneStart());
    64.                     StartCoroutine(myCharacterAnimationScript.CutSceneStart());
    65.                 }
    66.             }
    67.  
    68.         }
    69.         else
    70.         {
    71.             ePressText.SetActive(false);
    72.         }
    73.         RaycastHit CeilingButton;
    74.         Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    75.         if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
    76.         {
    77.             if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
    78.             {
    79.                 ePressText.SetActive(true);
    80.                 if (Input.GetButtonDown("actButton"))
    81.                 {
    82.                     RelocateScript gas = GetComponent<RelocateScript>();
    83.                     CeilingButton.collider.GetComponent<RelocateScript>().Relocate();
    84.                 }
    85.                 Debug.Log("hitCollider");
    86.             }
    87.             if (CeilingButton.transform.gameObject.collider.tag == "Note")
    88.             {
    89.                 ePressText.SetActive(true);
    90.             }
    91.             if (CeilingButton.transform.gameObject.collider.name == "EndingDoorDead")
    92.             {
    93.                 ePressText.SetActive(true);
    94.                 if (Input.GetButtonDown("actButton"))
    95.                 {
    96.                     DeadEnd cas = GetComponent<DeadEnd>();
    97.                     CeilingButton.collider.GetComponent<DeadEnd>().LoadDeadEnd();
    98.                 }
    99.             }
    100.             if (CeilingButton.transform.gameObject.collider.name == "EndingDoorLife")
    101.             {
    102.                 ePressText.SetActive(true);
    103.                 if (Input.GetButtonDown("actButton"))
    104.                 {
    105.                     LifeEnd las = GetComponent<LifeEnd>();
    106.                     CeilingButton.collider.GetComponent<LifeEnd>().LoadLifeLvl();
    107.                 }
    108.             }
    109.             if (CeilingButton.transform.gameObject.collider.tag == "hand")
    110.             {
    111.                 ePressText.SetActive(true);
    112.                 if (Input.GetButtonDown("actButton"))
    113.                 {
    114.                     BlackHandScript BlackHand = GetComponent<BlackHandScript>();
    115.                     CeilingButton.collider.GetComponent<BlackHandScript>().GetAudioInDialog();
    116.                 }
    117.             }
    118.             else
    119.             {
    120.                 ePressText.SetActive(false);
    121.             }
    122.         }
    123.     }
    124. }
    This raycast script gets this script:

    Code (CSharp):
    1. public class BlackHandScript : MonoBehaviour {
    2.     public DialogSoundPlay PlayBlackHand;
    3.     public void GetAudioInDialog ()
    4.     {
    5.         DialogSoundPlay pas = GetComponent<DialogSoundPlay>();
    6.         PlayBlackHand.GetComponent<DialogSoundPlay>().BlackHandSound();
    7.     }
    8. }
    And BlackHandScript gets the last one that have to play a sound. It alsow attached to a player whit AudioSources.

    Code (CSharp):
    1. public class DialogSoundPlay : MonoBehaviour {
    2.     public AudioClip StartLevelDial;
    3.     public AudioSource DialogSource;
    4.     public AudioClip TommyTriggerClip;
    5.     public AudioClip BlackHandClip;
    6.     void Start () {
    7.         StartLvlDial();
    8.     }
    9.     void StartLvlDial ()
    10.     {
    11.         DialogSource.PlayOneShot(StartLevelDial);
    12.     }
    13.     public void TommyTriggerDialog ()
    14.     {
    15.         DialogSource.PlayOneShot(TommyTriggerClip);
    16.     }
    17.     public void BlackHandSound ()
    18.     {
    19.         DialogSource.PlayOneShot(BlackHandClip);
    20.     }
    21. }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    At a guess, i would say that the audio source is disabled
    Look at it in the inspector, if the component is not ticked, it is disabled
     
  3. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    It's eneabled.