Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

access sound clips through scripts

Discussion in 'Audio & Video' started by smokingbunny, Dec 7, 2014.

  1. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    hello folks,

    one thing id like to know is accessing audio clips through scripting. now, i dont mean where i drag over an audio clip to an object and then using something like this:
    Code (csharp):
    1.  
    2. if(Input.GetButtonDown("Jump")){
    3.       if(!audio.isPlaying){
    4.             audio.Play();
    5.        }
    6.  }
    7.  
    what im looking to do is use code to access the sound from the folder its stored in. maybe something like this:
    Code (csharp):
    1.  
    2. if(Input.GetButtonDown("Jump")){
    3.       if(!audio.isPlaying("Audio/jumpSound01")){
    4.             audio.Play();
    5.        }
    6.  }
    7.  
    or can someone help in accessing multiple sound from one object, using something like the first code snippet. i cant seem to find these things at all. its just for one sound, which has been useful, but need something for many more sounds for different things

    thanks
     
  2. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    also i do know about the use of sounds within the animation pane. which is cool.
    but just need something where i can access the folder where things are stored to play
     
  3. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    ok, ive re-worked it to have drag and drop. now im running into a error when using this type of code. it says:
    ------
    MissingComponentException: There is no 'AudioSource' attached to the "objPlayer" game object, but a script is trying to access it.
    You probably need to add a AudioSource to the game object "objPlayer". Or your script needs to check if the component is attached before using it.
    objPlayer.Update () (at Assets/Scripts/objPlayer.cs:63)
    ------
    Code (csharp):
    1.  
    2. public AudioClip jumpSound;
    3.  
    4. if(!audio.isPlaying){
    5.    audio.clip = jumpSound;
    6.    audio.Play();
    7.  }
    8.  
    but what i have done is a drag and drop of the audio onto that audio.clip that i coded in. so i just then drag in what sound i need in unity for the jump control. but im running up dry. i figured that this way would be best to move ahead since i can then access and set sounds to what i need

    thanks
     
  4. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    280
    AudioClips just store the sound.

    You need to add an AudioSource component to the object and add the clip to it.
     
  5. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,303
    do as the error message says: attach AudioSource component on the GO.
    as for the 'many clips' part, that you have correct
     
  6. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    ah ha.
    though i have tried
    Code (csharp):
    1.  
    2. AudioSource jumpSound;
    3.  
    4. audio.clip = jumpSound;
    5. audio.Play();
    6.  
    and it doesnt work. i am still a beginner at this, but cant think of what to do

    thanks for your answers and help
     
  7. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,303
    set it to 2D sound in import setting or move close to the audio listener ( by default main camera )
     
  8. smokingbunny

    smokingbunny

    Joined:
    Jul 24, 2014
    Posts:
    98
    aaah, got it now.

    thanks for the help people