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

Hi,

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

We posted below but no answers:

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

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.

1 Like

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

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.

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RunInWindow : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        if (GlobalVariables.RunInWindowDone == false)
        {
            GlobalVariables.RunInWindowDone = true;
            Screen.SetResolution(360, 640, false);

            GlobalVariables.MusicToPlay = 0;

            AudioClip audioClip = null;
            AudioSource audioSrc = null;

            audioClip = Resources.Load<AudioClip>("Scenes/Title-BGM");
            audioSrc.clip = audioClip;
            audioSrc.volume = 1.0f;
            DontDestroyOnLoad(audioSrc);
            audioSrc.Play(1);
        }
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

“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

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RunInWindow : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        if (GlobalVariables.RunInWindowDone == false)
        {
            GlobalVariables.RunInWindowDone = true;
            Screen.SetResolution(360, 640, false);

            GlobalVariables.MusicToPlay = 0;

            AudioSource audioSrc = null;

            audioSrc = Resources.Load<AudioSource>("Scenes/Title-BGM");
            audioSrc.volume = 1.0f;
            DontDestroyOnLoad(audioSrc); //Not working???
            audioSrc.Play(1);
        }
    }

    // Update is called once per frame
    void Update()
    {
    
    }
}

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:

4734212--448574--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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RunInWindow : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        if (GlobalVariables.RunInWindowDone == false)
        {
            GlobalVariables.RunInWindowDone = true;
            Screen.SetResolution(360, 640, false);

            GlobalVariables.MusicToPlay = 0;

            AudioClip audioClip = null;
            AudioSource audioSrc = null;

            // Not working below?
            audioClip = Resources.Load<AudioClip>("Title-BGM");
            audioSrc.clip = audioClip;
            audioSrc.volume = 1.0f;
            DontDestroyOnLoad(audioSrc);
            audioSrc.Play(1);
            // Not working above?
        }
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

Don’t use Resources.Load to get the audio source. Make it the same object that this script is on, and just use GetComponent(). 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.

Ok, it’s working - thanks for the assistance…

Project download has been updated:
http://fallenangelsoftware.com/stuff/files/FerrariF40-Engine/FerrariF40-Engine.zip