Help with audio 2d
Hello i'm making a 2d game but i have some problems with sound,everytime i try to play a sound it says "Can not play a disabled audio source".What does that mean? Here is my code
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col){
public AudioClip des;
if(col.gameObject.tag == "ball"){
Destroy(gameObject);
GetComponent<AudioSource>().clip = des;
GetComponent<AudioSource>().Play();
}
}
}
Answer by sys12 · Sep 22, 2015 at 02:32 PM
It's funny. You are trying to get the AudioSource from a gameobject which you destroyed before. Why do you want to destroy this gameobject?
Maybe you want to destory col.gameobject.
yeah i just figured that out lol,btw i'm making a brick break type game i set when the collider of the ball hits the brick it destroys it , but meanwhile i would like to play a sound so i tried something like this in 2 scripts but won't work either
first script
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "ball") {
GetComponent<BrickScript>().OnBrickDestroyed();
Destroy(gameObject);
}
second script
public void OnBrickDestroyed(){ GetComponent().clip = des; GetComponent().Play ();
}
I think you should attach a script to you ball to handle the sound. That is when the brick is hit, send a notification to your ball before the brick got destroyed and let you ball to play the sound.
still nothing it says everytime "NullReferenceException : Object not set to an intance of an object brick.Sound.OnCollisionEnter2D"
Your answer
Follow this Question
Related Questions
Second AudioClip won't play 0 Answers
How to stop current audio before another audio starts? 0 Answers
How to reduce delay when playing sound 0 Answers
How can i select music from mobile that will play in unity audiosource? 0 Answers
Some sounds won't play 0 Answers