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];

}

}

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. :slight_smile: