- Home /
Taking too long for replys, will ask a new quesion.
Playing Music After Seconds Works, But When Scene Reloads, Script Doesn't Work
Hello!
I have a probably really easy question but I have no Idea what do to. In my scene, I don't have the music start or a GUI text disappear in till three seconds after the scene starts. So when I start my scene, everything works, but if I die, then I go back to the Main Menu, where I can select the same scene and try it again, but for some reason, the music starts right away, even though the GUI text still stays for three seconds then disappears. Here is the music script, don't know if it will be much help.
var song : AudioSource;
function Update () {
if (!song.isPlaying && Time.time >= 3) {
song.Play();
}
}
Any Thoughts?
Answer by UsmanAbbasi · May 02, 2016 at 02:21 PM
Time.time gives you the time since the projects is started. So when you reload your scene, Time.time is always greater than 3 which causes the condition to be true and it plays the music.
You need to have another variable to store your time since the scene is started and always initialize it to 0 in Start() method.
Or you can use Invoke() to play song after 3 seconds like this.
void Start() { Invoke("PlayMusic" , 3); } void PlayMusic() { song.Play(); }
Ok, two things. Do I add your script to my script, or do I delete $$anonymous$$e and put yours there? Also, I am getting two errors, even if I Delete my script or not, saying "Void is not a valid $$anonymous$$acro".
Any $$anonymous$$gestions?
because letter v
of your void
is capital, make it small
It is a small letter.
var song : AudioSource;
function Update () {
if (!song.isPlaying && Time.time >= 3) {
song.Play();
}
}
void Start()
{
Invoke("Play$$anonymous$$usic" , 3);
}
void Play$$anonymous$$usic()
{
song.Play();
}