Question by
Peremy · Jul 29, 2020 at 01:55 PM ·
coroutineaudiosourceaudioclip
How would I check if an audioclip assigned to another object is playing?
Basically, I need to check if an audioclip is playing on another object so that I can use it to activate a coroutine. I'm basically not sure how I grab the audiosource, and check if the clip is playing. I'm also a bit of a newbie still and i feel this should be easy to figure out but I've really got no idea!! If anyone has any thoughts I'd really appreciate the input, thank you!
Here is the script that plays said audio - I only want it played once, hence why I'm using PlayOneShot with the audiosource assigned to the object as a component.
public class PlayMarmion : MonoBehaviour
//put on GoblinGo object to play Marmion extract reading sequence
{
public AudioSource poem2;
public AudioClip creepy;
AudioSource creepyVocals;
public GameObject scottAnno;
void Start()
{
creepyVocals = GetComponent<AudioSource>();
}
private void OnMouseDown()
{
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray2) & CompareTag("Goblin"))
{
creepyVocals.PlayOneShot(creepy, 0.05f);
poem2.PlayDelayed(8);
scottAnno.SetActive(true);
}
}
}
and here is the script im trying to check the audio with - it's assigned to a piece of UI i want to fade so I need to check if the audio is playing before I start the coroutine:
public class FadeSpookyBorder : MonoBehaviour
//attach to SpookyBorder
{
// Start is called before the first frame update
public CanvasGroup uiElement;
public GameObject spookyBorder;
private Animator animator;
public GameObject goblinGO;
private float start = 0;
private float end = 1;
private float lerpTime = 3f;
void Start()
{
animator = spookyBorder.GetComponent<Animator>();
uiElement = GetComponent<CanvasGroup>();
goblinGO = GameObject.FindWithTag("Goblin").GetComponent<AudioSource>(); //I know this is incorrect and I'm not sure how to grab the clip
}
private void BorderFade()
{
if //some reference to if the audioclip is playing or not
{
//spookyBorder.SetActive(true);
//animator.Play("SpookyEdgesAnimation");
UIFader fader = Camera.main.GetComponent<UIFader>(); //UIFader script attached to main camera
fader.StartCoroutine(fader.FadeCanvasGroup(uiElement, start, end, lerpTime));
}
}
}
Comment