Coroutine on collision
I am trying to play a sound which is attached to a mixer when something is hit by a ball. The code works without the coroutine and no destroy gameobject in the code, but the brick needs to delete when hit and i think my code is the best way to do it (although i am probably wrong). But the problem is that i can't get the coroutine to start on a collision with an object. I removed the collision part of the code i was trying in order to allow the code to work but now all the objects this is attached too delete after 1 second.
How can i start a coroutine on a collision or if that is not possible what is a work around.
Thanks for any and all help.
using UnityEngine; using System.Collections;
public class Bricks : MonoBehaviour {
 public GameObject brickParticle;
 public AudioClip Brick_breaking;
 private AudioSource SoundFx;
 void Awake()
 {
     SoundFx = GetComponent<AudioSource>();
     StartCoroutine(Example());
 }
 IEnumerator Example()
 {
     SoundFx.PlayOneShot(Brick_breaking, 1F);
     Instantiate(brickParticle, transform.position, Quaternion.identity);
     GM.instance.DestroyBrick();
     yield return new WaitForSeconds[1];
 }
}
If you destroy the object which has the audio source, the audio source will stop playing.
Answer by Cepheid · Nov 07, 2015 at 10:02 PM
Hi there @meechan007 To start a coroutine within a collision all you should need to put is the following:
     public GameObject brickParticle;
     public AudioClip Brick_breaking;
     private AudioSource SoundFx;
 
     void Awake()
     {
         SoundFx = GetComponent<AudioSource>();
     }
 
     void OnCollisionEnter (Collision other)
     {
         StartCoroutine("Example");
     }
 
     IEnumerator Example()
     {
         SoundFx.PlayOneShot(Brick_breaking, 1F);
         Instantiate(brickParticle, transform.position, Quaternion.identity);
         GM.instance.DestroyBrick();
         yield return new WaitForSeconds(1);
     }
If think the issue you may be having might be due to you using two [ ] angle brackets for your WaitForSeconds statement rather than two ( ) parenthesis. I hope it helps! If not please feel free to tell me and I'll try to help further. :)
Your answer
 
 
             Follow this Question
Related Questions
Missile Boucne Issues (need a creative solution) 0 Answers
Object lerping forwards a set distance every 3 seconds. 1 Answer
How to reduce lag in coroutines - specifically WaitForSeconds()? 1 Answer
WaitForSeconds returns control to calling routine before it completes? 0 Answers
Could not load source 'Coroutines.cs': No source available. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                