- Home /
Why PlayOneShot is not working? C#
I have tried everything, but I can't find why. Here is the script:
public AudioClip clip;
private AudioSource fx;
GameObject music;
void Awake() {
fx = GameObject.Find("_GM").GetComponent<AudioSource>();
music = GameObject.Find("_music");
}
IEnumerator ChangeLevel () {
float fadeTime = GameObject.Find("_GM").GetComponent<Fading>().BeginFade(1);
yield return new WaitForSeconds(fadeTime);
SceneManager.LoadScene(2);
}
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == "center") {
StartCoroutine(ChangeLevel());
fx.PlayOneShot(clip, 1f);
Destroy(music);
}
}
}
BTW its not playing at all, but audio.Play() works well, but I need playoneshot for my game!
Do you have the AudioClip clip assigned to this script in the inspector, any errors? Because the AudioSource.Play() plays the one assigned to audiosource, I think.
No - what I meant was - is the OnTriggerEnter callback in your code getting called? Does the music stop and scene change as expected?
Yes it changes the scenes normally and destroys music so its strange.
Is it a 3D sound? Where is the _G$$anonymous$$ object in relation to your AudioListener? Is it simply too far away to hear?
Not use Awake for things like GameObject.Find because object can not exist at those moment, use awake for getcomponent on this object not other, so try move gameObject.Find to Start ins$$anonymous$$d of Awake. Not sure this is core of your problem but can be.
Could it just be that the scene change is happening too quickly? What value is being returned for fadeTime at line 11?
Answer by Aappo · Oct 04, 2016 at 08:51 PM
Nothing worked :( if it is a bug?
your code does work without any problems, given that your
void Awake() {
fx = GameObject.Find("_G$$anonymous$$").GetComponent<AudioSource>();
music = GameObject.Find("_music");
}
DOES find something and the correct clip was assigned.
have you tested these already? :
checked, if the tag "center" is assigned to the object you plan colliding with
call fx.PlayOneShot(clip, 1f) before starting the coroutine
comment out the coroutine part completely
comment out Destroy(music)
Your answer
Follow this Question
Related Questions
Sound wont play when triggered (javascript) 1 Answer
Looping an audio while holding "shift key" 1 Answer
Problems combining Audio triggers, PlayOneShot and Audio Arrays 1 Answer
How to switch Audiosource without stacking by an input? 0 Answers
Audio does not finish playing whenever I press another key 0 Answers