- Home /
Sound plays when instantiated and not on trigger.
I want my sound clip to play wen collision, but sound plays at the time the prefab the script is attached to is instantiated. Any ideas out there?? …please?? Here´s my code.
public class Collision : MonoBehaviour {
public AudioClip crash;
void OnTriggerEnter2D (Collider2D other){
if (other.tag == "Player") {
other.GetComponent<AddForce>().controll = true;
AudioSource.PlayClipAtPoint(crash, transform.position);
Destroy (other.gameObject, 3f);
Application.LoadLevel ("Scene1");
ScoreBoard.scoreP2 = 0;
}
}
}
A script won't run on a game object that is not enabled. So, if it hasn't been instantiated, then the script will not run. You might want to call the sound from the other collider in the trigger
Answer by wibble82 · Mar 05, 2014 at 09:26 PM
Hi there
Does your prefab have an audio source attached with 'Play On Awake' set to true? If so that would cause it to play whatever sound was selected as soon as the object was instantiated.
Also, loading a level (which unloads anything you havent marked as 'dont unload') immediately after telling the sound to play seems a little dodgy to me. I can't say for certain it would cause problems, but if your sound is failing to play it might be worth seeing what happens if you remove that line of code.
-Chris
Thanks Chris. That did it, and as you assumed, that level load had to be changed. Asle
Your answer
Follow this Question
Related Questions
Help Guitar Hero Style or similar 0 Answers
Trigger sound doesn't work 0 Answers
Trigger sound when walking through cube trigger 3 Answers
trigger sound by left mouse click ? 1 Answer
Get an object to play a sound if player rolls over a trigger. 2 Answers