I am working on a Bubble Popping Game for Android and had to find the answer to this question via a C# tutor.
Here is a step by step Guide on how to play a sound file & destroy an object or prefab:
1.) Find the sound you want to use in your File Explorer and drag it into your Unity Assests Folder in the Project View. (TIP: You may download Audacity for free and trim the audio clip prior to importing it to Unity.)
2.) On the game object you wish to have the sound play on (or prefab you wish to play the sound on) selected in Hierarchy, you want to open the Inspector tab and Add Component > Audio > Audio Source.
3.) Now, with your object selected in Hierarchy look in the Inspector under the Audio Source component you added in the previous step, click the little circle and select the sound file you imported in step 1.
4.) Be sure to Uncheck Play On Awake.
5.) Right Click on your Assets Folder in the Project View > Create > C# Script and name it whatever you want.
6.) Cut and paste the following code:
public class YourScriptNameHere : MonoBehavior
public Renderer rend;
public void Start()
rend = GetComponent<Renderer>();
rend.enabled = true;
public void YourMethodNameHere()
AudioSource audio = GetComponent<AudioSource>();
rend.enabled = false;
+7.) Save the Script & Attach it to your object. Locate the script in your Assets Folder. First, Click on the Object in the Hierarchy. Then drag your Script onto the selected Object.
+8.) There is no call to this method currently. Assuming you already created an Input Method you would just need to add the call for this method. EXAMPLE:
YourMethodNameHere(); should be placed inside your call method.
IF NOT, Continue reading…
Creating a CALL for the method:
This is for Touch Input using an Android.
1.) Lets create a new C# Script and name it “BubbleBehavior”.
2.) Copy & Paste the Above Code into this new C# Script.
BubbleBehavior AND Change
TouchPop() +NOW SAVE THE SCRIPT & ATTACH IT TO YOUR OBJECT
5.) Create another C# Script and name it “TouchDestroyer”.
6.) Copy & Paste the following code into TouchDestoryer.cs
public class TouchDestroyer : MonoBehaviour
void Update ()
foreach (Touch touch in Input.touches)
if (touch.phase == TouchPhase.Began)
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
if (Physics.Raycast (ray, out hit, Mathf.Infinity))
BubbleBehavior bb = hit.transform.gameObject.GetComponent<BubbleBehavior> ();
if(bb != null)
Finally, Drag the TouchDestroyer script onto your Main Camera in the Hierarchy View.
Now you should be able to tap, play audio, disable renderer, and destroy your game object. In short, you can now play a sound when your object is destroyed.
I hope this helps!