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…
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.
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…
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()
{
}
}
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.