- Home /
How to wait until everything is loaded before starting?
My game starts playing music before the unity player progress meter is even finished. Is there a way to know when everything is done loading before the game does anything?
Answer by Gillissie · Mar 23, 2011 at 06:41 AM
I'm quite certain that this delay is actually due to the GUI delay that I asked about in this other question: http://answers.unity3d.com/questions/46508/unitygui-display-delay
Answer by loopyllama · Mar 11, 2011 at 08:01 PM
you cannot know. even if the level finished loading you could have a bunch of scripts that pool objects, instantiate gameObjects, or other intensive tasks. depending on the computer or device's power, these tasks will finish at different rates.
I say cheat. play the music on a coroutine that waits for a second or two, or as long as your safely need. that way you know the level will be finished loading and things will get going before the music starts...
If somebody have a bunch of scripts doing this and that THEY can know that they do.
Answer by Bunny83 · Mar 11, 2011 at 07:07 PM
Never had such a behaviour. What player do you talk about? Standalone or webplayer? If you build a webplayer do you use the "streamed" option? Because with a streamed web build unity starts playing after the first level is streamed. You have to check yourself if everything is loaded via Application.GetStreamProgressForLevel.
Where do you start your music? Just an audiosource with a looped audioclip? How long before (the progressbar disappears) do you hear the music? Some more information on your setup could be helpful.
edit
If you use ExecuteInEditMode and you want to start the sound in the Start() method, just check for Application.isPlaying. It's always true for all kinds of builds, but inside the Editor it's just true when in PlayMode
.
Web player. The Audio Source is in the scene and set to "Play on Awake". It usually starts playing 1-2 seconds before I actually see my scene.
If I uncheck "Play on Awake" and start the sound in the Start() method of my main script, then the sound plays a bunch of times in the editor because I have @script ExecuteInEdit$$anonymous$$ode() enabled for my GUI stuff. I suppose I could split the script, but it's such a hassle for scripts to talk to each other because you have to set up all the object references. $$anonymous$$aybe there's a best practices doc for Unity scripting?
Take a look at Application.isPlaying. I've changed my answer.
Thanks for the tip. I tried that but it still starts playing music before anything is displayed. There is a 1-2 second delay between the unity progress meter and actually seeing something (screen is plain white during this time), but the music starts immediately. I have a feeling it's something I'll just have to live with.
Have you ever fixed the problem? The same thing is happening to me
Answer by Niklas · Mar 11, 2011 at 07:43 PM
I would recomend you to set up a loading state in your first scene where you wait for level loading and other async processes that needs to be done before you create your object that plays music.
If you load your level with
Application.LoadLevel();
you need to wait until
Application.isLoadingLevel
is false before you start your music
he said that music starts before progress bar finishes so it's first level (no level loading).
Then he could do like !IsLoadingStuff. The same point applies
Answer by Statement · Mar 11, 2011 at 07:47 PM
Are you starting the music in Awake() (PlayOnAwake)?
If so, maybe you should try doing that in Start() instead? Awake is called when the object is created and Start is called before the first frame is executed. Thus the object might start playing while its loading. Having it in Start might allow for the game to fully load before it is called.
void Start()
{
audio.Play();
}
Your answer
Follow this Question
Related Questions
Change LoadType of an AudioClip at runtime to StreamFromDisc 0 Answers
Problem with GetStreamProgressForLevel 0 Answers
Difference between LoadLevelAdditive and LoadLevelAdditiveAsync 1 Answer
How to speed up loading and optimize my game? 1 Answer
How to show an image while waiting for the game to finish loading 0 Answers