Search Unity

Need help making sense of AudioSource.Play API

Discussion in 'Scripting' started by Borderline Chaos, Aug 28, 2015.

  1. Borderline Chaos

    Borderline Chaos

    Joined:
    Aug 7, 2012
    Posts:
    47
    This is my main issue with people simply linking to the API when someone's having trouble doing something. The API's mostly lack context and when you're still unfamiliar with all the static methods and such it's hard to tell what is already part of Unity and what we need to add. My best example is that you MUST have a Resources folder to use Resources.Load. You MUST add this folder as it is not inherent in a scene's creation nor does the API say you must manually create this folder.

    Now onto this simple script:

    I want to change background music using a script not the inspector. All the other code I've seen doesn't use the API so I assume it's what most "experts" would say is wrong. I look at the API and I don't see much context.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class ExampleClass : MonoBehaviour {
    6.     void Start() {
    7.         AudioSource audio = GetComponent<AudioSource>();
    8.         audio.Play();
    9.         audio.Play(44100);
    10.     }
    11. }
    I'm still a beginner so when I run across [RequireComponent(typeof(AudioSource))] I'm not sure what this means. I've never seen this in any of the C# code the other help/answer threads have regarding playing audio with a script. Since this is in the API and no one has it in their code I assume they're not doing it right. I still don't know what it does.

    AudioSource audio = GetComponent<AudioSource>(); I get that I'm referencing an AudioSource from outside this script so I have to use the GetComponent to assign it to my audio variable. I'm not sure if this <AudioSource> is the same as the Audio Source I added to my camera or an entirely new GameObject that is internal to Unity. The audio source on my camera is Audio Source, not AudioSource so I assume they're not the same.

    audio.Play(); Well, since I've not indicated what clip to play, this won't play anything. To top it off, I get the error"`AudioSource' does not contain a definition for `Play'" when I copy the API into a script and try to use it.

    I'm really trying to learn on my own and not seem like the person who wants others to do their work for them but when most of the code I'm looking at are specific answers to people not following the API I don't think I'm learning the right way of doing things. If I can't follow this simple piece of code I think I'm going to just be like everyone else and write bad code.
     
  2. DenisLemos

    DenisLemos

    Joined:
    May 1, 2015
    Posts:
    787
    All GameObjects need the component "Audio Source" to be able to play a sound in the game, be it an effect or background music.

    So this line of code, makes it added component "Audio Source" for GameObject that receive this script. With the intention of avoiding errors if the developer forget to add manually.

    This get all the "information or property" of the component "Audio Source" that is added at the same object that is the script, and saves this information in the variable "audio" to be used later without having to be accessing the component at all time.


    This play the audio selected in the "AudioClip" property in the Inspector. In the AudioSource component


    If you want to play a different sound than are selected in the property "AudioClip", you should create a variable of type AudioClip and drag the audio into it in the script inspector.
    Exemple:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class ExampleClass : MonoBehaviour
    6. {
    7.     public AudioClip startSound;
    8.     AudioSource audio;
    9.  
    10.     void Start()
    11.     {
    12.         audio = GetComponent<AudioSource>();
    13.         audio.PlayOneShot(startSound, 0.7F);
    14.     }
    15. }
     
    Last edited: Aug 28, 2015
    Borderline Chaos likes this.
  3. Borderline Chaos

    Borderline Chaos

    Joined:
    Aug 7, 2012
    Posts:
    47
    Sorry for the delay in responding, work and all that. Thanks for the explanation.