- Home /
2d game, audio not playing
Hey guys, I've been fighting with this and can't see my problem. I have the AudioSource attached, i am getting no error messages, and we are hitting both debug strings but the audio isn't playing. if i set the audio to play on awake i can hear it when the coins spawn so i know the clip is good.
using UnityEngine;
using System.Collections;
public class moveCoin : MonoBehaviour {
private int moveSpeed = -3;
public AudioSource audio;
void Start () {
Invoke ("destroyCoin", 15);
}
void Update () {
this.transform.position += (new Vector3 (moveSpeed, 0,0) * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other){
playerControll scrip = other.GetComponent<playerControll> ();
if(scrip!=null){
scrip.bumpCoinCount();
Debug.Log("before");
audio.Play();
Debug.Log("after");
}
Destroy (this.gameObject);
}
void destroyCoin(){
Destroy (this.gameObject);
}
}
and here is a screen cap of the audiosource settings.
Answer by Berge · Jul 27, 2015 at 01:51 PM
On line 26 you are destroying the game object. Is the AudioSource component placed on the same game object? In that case, the component (and the rest of the object) is destroyed before it can play the sound. Use invoke and call your destroyCoin method with enough delay for the sound to be played.
Your answer
Follow this Question
Related Questions
hi.how save all sounds in my scene? 1 Answer
Max amount of simultaneous, hearable sounds? 1 Answer
Realistic car sound? 0 Answers