Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Play audio sound on mouse click.

Discussion in 'Scripting' started by bonii, Mar 20, 2010.

  1. bonii

    bonii

    Joined:
    Jan 27, 2010
    Posts:
    84
    I know how to do a mouse click. But whenever I press my mouse, it shoots a bullet. I need it to play the gun shot audio file. I already imported it. How do have the script play it on a mouse click?
     
  2. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Like this.

    Code (csharp):
    1.  
    2. var handGunShot : SoundClip;
    3.  
    4. function Update(){
    5. if (Input.GetButton("Fire1"){
    6. //shoot the bullet
    7. audio.PlayOneShot(handGunShot);
    8. }
    9. }
     
  3. JeremyGames

    JeremyGames

    Joined:
    Jul 4, 2014
    Posts:
    21
    I know this is outdated but here.

    var Portalfire1 : SoundClip;//Replace"Portalfire1"withyour sound

    function Update(){
    if (Input.GetButton("Fire1")){
    //shootthe bullet
    audio.PlayOneShot(Portalfire1); //Replace"Portalfire1"withyour sound
    }
    }