- Home /
Playing Music Problem.
Hello!
So my script plays music after 3 seconds after you click play, but when the scene changes, and you come back to the scene wit the music, it starts right away, because the game has been playing for longer then 3 seconds. But I am getting an error saying that "void is not a valid macro." Someone on the last thread said it was because I had capitals, but I did not. Here is the script:
var song : AudioSource;
function Update () {
if (!song.isPlaying && Time.time >= 3) {
song.Play();
}
}
void Start()
{
Invoke("PlayMusic" , 3);
}
void PlayMusic()
{
song.Play();
}
Hopefully some can help me out, I have been delaying the game because of this.
Answer by tanoshimi · May 08, 2016 at 07:02 AM
Your script contains a mixture of C# syntax:
void Start ()
And Javascript syntax:
function Update ()
You need to fix this before the script will compile.
Answer by srylain · May 08, 2016 at 05:32 AM
Use a couple more variables.
var song : AudioSource;
var timeSinceClickedPlay : float;
var timeUntilStartSong : float = 3f;
function Update () {
if (!song.isPlaying && timeSinceClickedPlay >= timeUntilStartSong) {
song.Play();
}
else {
timeSinceClickedPlay += Time.deltaTime;
}
}
void Start()
{
Invoke("PlayMusic" , 3);
}
void PlayMusic()
{
song.Play();
}
Your answer
Follow this Question
Related Questions
Why sound doesn't stop? 2 Answers
Make a command run only once 1 Answer
How I can prevent music from stopping after changing a scene? 3 Answers