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

I don't know how to play sound in Unity3D

Discussion in 'Audio & Video' started by Temp10101, Apr 16, 2015.

  1. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Yes, I have never worked with Unity's sound system, I managed to do this:
    Code (CSharp):
    1. public class JustAmbience : MonoBehaviour {
    2.             public AudioClip backgroundMusic;
    3.             void Start() {
    4.                 audio.PlayOneShot(backgroundMusic);
    5.             }
    6.         }
    But it shouts that it's deprecated and I should use XYZ instead, so I finished up as:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class JustAmbience : MonoBehaviour {
    5.     public AudioClip backgroundMusic;
    6.     void Start() {
    7.         audio.GetComponent<AudioSource>(backgroundMusic);
    8.     }
    9. }
    Still exactly same error, and I don't know how to make it work. I saved file, no slots, just file included.

    Errors:
    Assets/JustAmbience.cs(7,17): error CS0619: `UnityEngine.Component.audio' is obsolete: `Property audio has been deprecated. Use GetComponent<AudioSource>() instead. (UnityUpgradable)'
    Errors:
    Assets/JustAmbience.cs(7,23): error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)' cannot be used with the type arguments

    But I did!
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    You did not read the error message properly. Component.audio is deprecated, and so you will also get the error when you use Component.audio.GetComponent(). Use GetComponent() on your own component (your script) directly. That is, drop the "audio." part.
     
  3. Jami_2

    Jami_2

    Joined:
    Apr 22, 2013
    Posts:
    19
    Chance this line


    Code (CSharp):
    1. audio.GetComponent<AudioSource>(backgroundMusic);
    2.    

    To this this
    Code (CSharp):
    1. audio.GetComponent<AudioSource>().Play (backgroundMusic);
    2.    
    Solve this your problem.
     
  4. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    163
    Also, make sure that you have added an Audio Source to the same game object that the script is attached to.
     
  5. lostvalleyguy

    lostvalleyguy

    Joined:
    Apr 18, 2015
    Posts:
    1
    I am also new to this but I finally got it to work on a trigger (which was my objective).

    1. I attached an audio source to the object and the clip to the audio source.
    2. I added this line of code to my script (also attached to the same game object)

    GetComponent<AudioSource>().Play();

    This worked for me and is the only one of the suggestions in here that compiles with my version of unity.

    Good Luck!
     
    Fantastic_m9 and Temp10101 like this.