All sounds instantly play on game start.
When I start my game, all the sounds play instantly once. They play like normal after that, but the random outburst really ruins the feel. I'm looking for a way to fix this, but I can't find any solution anywhere else. Here's my current script that plays audio... the script is applied to an object with AudioSystem for each individual sound, and then the AudioSystem objects are in turn applied to scripts that need to play them.
public class AudioScript : MonoBehaviour { public AudioClip MusicClip; public AudioSource MusicSource; // Use this for initialization void Start () { MusicSource.clip = MusicClip; MusicSource.Play(); } // Update is called once per frame void Update () { } }
Any help? I am new to Unity, and all my prior programming experience is in JS.
Answer by Sgt_Spike · Jun 09, 2018 at 04:50 PM
Are you sure you don't have the 'Play on Awake' option on in the Unity Editor for your audio sources? Having this option on will make that audio source play as soon as the object it is attached to becomes active.
Yes, I'm certain that none of my sounds other than my soundtrack have playOnWake set to true. Heres one of my audioObjects... https://imgur.com/a/7aUHD0i
Also, sorry for late reply, thanks for helping...
So you say these audioObjects are called from individual scripts when they are needed. Are the audioObjects (like the one you provided) always in your hierarchy or do they get added to the hierarchy during play mode from another script. If they are always in your hierarchy, there's your problem. You are playing the audio source in your Start() method, meaning as soon as that script becomes active, it is immediatly going to play the sound.
Ohh, that must be the problem then, thanks! How do you suggest I fix it most easily?
Answer by JulesVerny · Aug 23, 2021 at 03:15 PM
@Lukalotr I had a similar problem. Really frustrating as to why a particular Audio file was playing on startup, as it was no different to any of the other audio files.
I diagnosed it down to being that the name of this Audio File was the same as a GameObject, or named asset. So that Unity seems to get confused and played that Audio, because of its name clashed with a corresponding named assets. (They played correctly by script in the main game.
Once I renamed my Audio File(s) to be distinct, they did not play on Startup. So its now sorted.
This did help. I had the same problem (one file playing at start for no obvious reason) Renaming within unity did not help. I had do rename that file on the disk (to some unknown ending) and delete the .meta file. Then unity showed it up as white box, and it did not play. Changing its name back to a playable file format made it show up in unity again, and it is perfectly usable at all, but does not play at start,