- Home /
Cannot play a disabled audio source?
So I've been struggling over my code for a few hours with no luck. I'm trying to play and audio clip when walking into a box collider on top of a sword. But instead of playing the audio clip, I get the error "Cannot playa disabled audio source." It's very frustrating and I am quite confused. This is my code below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup2 : MonoBehaviour
{
public GameObject RealSword;
public GameObject FakerSword;
public AudioSource stab;
// Use this for initialization
void Start()
{
GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if (gameObject.tag == "MegaFake")
RealSword.SetActive(true);
FakerSword.SetActive(false);
stab.Play();
Any help is incredibly appreciated!
Answer by Xarbrough · Nov 10, 2017 at 11:52 PM
Is there any chance, the 'stab' AudioSource is part of the 'FakerSword' GameObject? If so, the error message shows, that you are disabling the sword object (and also the AudioSource with it), but then, you are trying to play it, which is not allowed. Or there might be some other situation in which the GameObject of the 'stab' source is disabled before playing.
To fix, put the AudioSource on a GameObject, which remains active while playing.
Also, you probably want to put curly braces around some of the lines following the if statement. Remember, that indentation doesn't matter and currently lines 30 and 31 will always run, even if the tag is not 'MegaFake'. ;)