Adding music to game

Good day all!

I have been looking all day to find tutorials on how to make a script when the scene launches the sound automatically plays. Also on how to trigger sounds on collision. I know that I need to use the Awake function on the 1st problem ,but I don’t know how play the sound on script and loop it as long as the scene is awake. I can’t seem to find tutorials and I am so new in coding in unity. Any help or guidance is much appreciated. Thank you all! :slight_smile:

Unless there’s more to it than what you described, the AudioSource ‘loop’ and ‘play on awake’ fields should take care of the first problem.

For the second problem, look at the various collision callback functions, such as OnTriggerEnter() and OnCollisionEnter().

Thank you for the response. I get your point and I see the structure of the script. My only problem now is doing the actual statement. Could I kindly ask for a complete script that do both problems? Thank you.

Example:

var myClip | AudioClip;

Awake()
{
the actual statement which I am clueless to construct with loop and play
}

Regarding the first problem, you don’t need to implement the Awake() function at all. Just check the ‘loop’ and ‘play on awake’ boxes in the inspector for the AudioSource component, and that should take care of it.

Thank you! I will try and test it soon.

And if you want the music to be the same loudness then make sure your music is NOT a 3D sound. To do that, select the music in the assets window, and uncheck to is a 3D sound box and press apply! :smile:

I will bear that in mind, Thank you!