Search Unity

New To Unity - Stuck On Implementing Audio - Good Tutorial Somewhere?(SOLVED)

Discussion in 'Getting Started' started by JeZxLee, Jul 10, 2019.

  1. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    Hi,

    Trying to build our 1st game with Unity.
    Really stuck on audio at this moment.

    We posted below but no answers:
    https://forum.unity.com/threads/sou...-integrating-into-project-with-source.707204/

    We need simple audio in Unity with following characteristics:
    - 1 channel mono audio
    - sound effect playing with user controlled volume
    - music playing(survives scene change) with user controlled volume

    That's it.
    We plan on making simple 2-D games with Unity for Internet and Android platforms.
    Been at this audio in Unity for some time and have reached frustrated levels.
    Audio on Unity is difficult to implement from our perspective...

    Jesse
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, first, don't get a "Sound Manager" asset. You don't need it. The needs you describe above are very simple:

    1. Add an AudioSource component to some object in your scene. Attach the desired sound effect.
    2. Call the Play method when you want it to play. Assign to Volume when you want to change the volume.
    3. If you want it to survive the scene change, make sure that object is at the root level of the scene, then call DontDestroyOnLoad on it.

    That's it.
     
    Joe-Censored likes this.
  3. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    Sounds good...
    How would we load(cache) all musics and sound effects once at game's 1st load?
    (cached audio should be accessible from any scene)
    Thanks!

    Jesse
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Any asset you reference in a scene is loading from disk when that scene loads. So my usual approach is to just include the GameObject which references all of the sound clips in an early scene, and set DontDestroyOnLoad, so it is available in all subsequent scenes. Music files can be very big, so I usually create a loading scene I run first which async loads the scene which will take a while.
     
  5. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    OK, we understand that better...

    We have a music audioclip we would like to play.
    We have the following C# source code but nothing is heard:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RunInWindow : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         if (GlobalVariables.RunInWindowDone == false)
    11.         {
    12.             GlobalVariables.RunInWindowDone = true;
    13.             Screen.SetResolution(360, 640, false);
    14.  
    15.             GlobalVariables.MusicToPlay = 0;
    16.  
    17.             AudioClip audioClip = null;
    18.             AudioSource audioSrc = null;
    19.  
    20.             audioClip = Resources.Load<AudioClip>("Scenes/Title-BGM");
    21.             audioSrc.clip = audioClip;
    22.             audioSrc.volume = 1.0f;
    23.             DontDestroyOnLoad(audioSrc);
    24.             audioSrc.Play(1);
    25.         }
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.        
    32.     }
    33. }
    34.  
    "RunInWindow" is a C# script that is run one time at the beginning of game...

    Any ideas?
    Thanks!

    Also you can download the complete project below if you need to look at it:
    fallenangelsoftware.com/stuff/files/FerrariF40-Engine/FerrariF40-Engine.zip

    Jesse
     
  6. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    OK, have audio(title screen music playing now)...
    Only issue is it stops playing the title music after scene change?

    Below is relevant source code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RunInWindow : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         if (GlobalVariables.RunInWindowDone == false)
    11.         {
    12.             GlobalVariables.RunInWindowDone = true;
    13.             Screen.SetResolution(360, 640, false);
    14.  
    15.             GlobalVariables.MusicToPlay = 0;
    16.  
    17.             AudioSource audioSrc = null;
    18.  
    19.             audioSrc = Resources.Load<AudioSource>("Scenes/Title-BGM");
    20.             audioSrc.volume = 1.0f;
    21.             DontDestroyOnLoad(audioSrc); //Not working???
    22.             audioSrc.Play(1);
    23.         }
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.    
    30.     }
    31. }
    32.  
    Project download below has been updated:
    http://fallenangelsoftware.com/stuff/files/FerrariF40-Engine/FerrariF40-Engine.zip

    Also a photo of Audio Source attached to scene:

    AudioSource-Title-BGM.png

    UPDATE:
    The only reason the music played was because the Audio Source had "Play On Awake" in the scene.
    I toggled off "Play On Awake" and the above code does nothing - no music is played?

    Little lost, hope someone can help:

    Jesse


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RunInWindow : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         if (GlobalVariables.RunInWindowDone == false)
    11.         {
    12.             GlobalVariables.RunInWindowDone = true;
    13.             Screen.SetResolution(360, 640, false);
    14.  
    15.             GlobalVariables.MusicToPlay = 0;
    16.  
    17.             AudioClip audioClip = null;
    18.             AudioSource audioSrc = null;
    19.  
    20.             // Not working below?
    21.             audioClip = Resources.Load<AudioClip>("Title-BGM");
    22.             audioSrc.clip = audioClip;
    23.             audioSrc.volume = 1.0f;
    24.             DontDestroyOnLoad(audioSrc);
    25.             audioSrc.Play(1);
    26.             // Not working above?
    27.         }
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.        
    34.     }
    35. }
    36.  
     
    Last edited: Jul 11, 2019
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Don't use Resources.Load to get the audio source. Make it the same object that this script is on, and just use GetComponent<AudioSource>(). Or else make a public AudioSource property on this script, and hook it up in the inspector.

    I recommend going through some of the beginner Unity tutorials, so you can learn the basics of how to manage and reference objects in a scene.
     
  8. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222