Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

a script that will select a random sound

Discussion in 'Scripting' started by Andreas Busk, Sep 25, 2014.

  1. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Hi All

    I am a musician and sound designer, teaching other musicians how to compose and design sounds for games.

    I am currently a scripting newbie to say the least.
    In the game 'Nightmares' by Unity, made for Unite 2014, the protagonist fires an automatic rifle.
    Can someone help me make a script that will select a random sound from a group of selected sounds each time the rifle goes off?

    This is to add variety and sonic depth to the feel of the weapon.

    Hope someone can help.

    Best,

    Andreas
     
  2. yyamiyyugi

    yyamiyyugi

    Joined:
    Jul 28, 2013
    Posts:
    37
    I would do something like:
    Code (CSharp):
    1.  int RandomGunSound() {
    2.         //Here I am assuming 3 sounds, it will pick 0, 1, or 2
    3.         int soundChosen = Random.Range(0, 3);
    4.         switch(soundChosen){
    5.              case 0:
    6.                    //PlaySound;
    7.                    break;
    8.              case 1:
    9.                    //PlaySound;
    10.                    break;
    11.              case 2:
    12.                    //PlaySound;
    13.                    break;
    14.          }
    15.     }
    16.  
    Then call this method whenever sound needs to be played, This may not be the best solution though.
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you already know how arrays work, you can put the sounds into an array and use something similar to
    Code (CSharp):
    1. int index = Random.Range(0, yourArray.length)
    in order to get a random index. Then simply play your sound using the index in your array.
     
  4. yyamiyyugi

    yyamiyyugi

    Joined:
    Jul 28, 2013
    Posts:
    37
    Much better solution
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class GunSound : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private AudioClip[] sounds; // populate in Inspector
    9.  
    10.     private bool isFiring;
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetMouseButtonDown(0))
    15.         {
    16.             isFiring = true;
    17.             StartCoroutine(ShootSounds());
    18.         }
    19.         else if (Input.GetMouseButtonUp(0))
    20.         {
    21.             isFiring = false;
    22.         }
    23.     }
    24.  
    25.     // randomly plays sounds as long as you hold down the left mouse button
    26.     IEnumerator ShootSounds()
    27.     {
    28.         while (isFiring)
    29.         {
    30.             var clip = sounds[Random.Range(0, sounds.Length)];
    31.             audio.PlayOneShot(clip);
    32.             yield return new WaitForSeconds(clip.length);
    33.         }
    34.     }
    35. }
    36.  
     
  6. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Thanks a lot guys! So kind of you to send me these solutions!

    Suddoha: Unfortunately I don't know how arrays work at all, so I'm afraid I can't use that solution right away, I do in fact know next to nothing about scripting.

    yyamiyyugi and KelsoMRK I open up MonoDevelop, copy-paste what you've done, save my script. But if I drag it into the Hierachy, Unity tells me that the script doesn't exist.

    Also I'm not certain what I should attach the script... on 'player' in Hierachy?

    Best wishes and thanks,

    Andreas
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If Unity tells you the script doesn't exist, it's most likely the name of the script which does not match the name of the class. E.g. if you use @KelsoMRK's script, you need to make sure the script is a C#-script and its name has to be 'GunSound'.
     
  8. yyamiyyugi

    yyamiyyugi

    Joined:
    Jul 28, 2013
    Posts:
    37
    Mine was a method, add it into your gun script and call it when you want the sound played However, I would probably take KelsoMRK's ShootSounds method and use that. If you add the ShootSounds and the AudioClip[] sounds to your gun script then you should beable to call the method when your firing without the need of a new script
     
  9. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Ok, thanks yyamiyyugi.

    I guess I have to ask then, where to copy KelsoMRK's script into the original script: before or after?

    I just deleted the original script and inserted his - bad idea I guess...
     
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Copy the whole script, place it into a C# script with the name GunSound. That should actually work as long as there isn't any mstake in the script.
     
  11. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Ok, will try that!
     
  12. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Ok excuse my stupid questions but where do I put the GunSound script once I've made it? What do I attach it to?
     
  13. Andreas Busk

    Andreas Busk

    Joined:
    Mar 21, 2014
    Posts:
    25
    Ok - Now I got this far - which is great progress!..but..when I play the scene and press fire button no sounds or shots or anything is seen or heard.
    What could I be doing wrong?
    Skærmbillede 2014-09-26 kl. 21.04.15.png
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The code assumed the GameObject you attached it to also had an AudioSource component.