Advertisement
Dvoyles

playsfx.cs

Jul 29th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /**
  5.  * STEPS:
  6.  * 1) Create an empty cube  and add it to the scene
  7.  * 2) Add 'AudioSource' to the cube
  8.  * 3) Add 'playsfx.cs'  to the cube
  9.  * 4) Add your sound effect (in my case, explosion.mp3) to that script
  10.  * 5) Add your sound effect (in my case, explosion.mp3) to your AudioSource
  11.  * 6) Hit 'play' in Unity
  12.  */
  13. public class playsfx : MonoBehaviour {
  14.  
  15.     // Add a reference to our sound effect
  16.     public AudioClip shootSound;
  17.  
  18.     // Where is this coming from? Did you add 'AudioSource' to your cube?
  19.     // Did you drag your .mp3 to the AudioSource?
  20.     private AudioSource source;
  21.  
  22.  
  23.  
  24.     // Use this for initialization
  25.     void Awake () {
  26.         source = GetComponent<AudioSource>();
  27.     }
  28.    
  29.  
  30.     // Update is called once per frame
  31.     void Update () {
  32.  
  33.         // If user hits left-mouse button.....
  34.         if (Input.GetButtonDown("Fire1"))
  35.         {
  36.             // Check if there is a sound effect...
  37.             if (shootSound == null) {
  38.                 // Throw a warning. Whoops, forgot to add a sound effect!
  39.                 Debug.Log("You forgot to add a sound effect! " + shootSound);
  40.             }
  41.  
  42.             // Play sfx
  43.             source.PlayOneShot(shootSound);
  44.         }
  45.    
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement